我正在使用Ubuntu中的套接字编程c++编写服务器客户端程序。
这是连接客户端到服务器的代码。
void setParent(string name,int parentPort){
struct addrinfo hints, *serverInfo , *rp;
int errcode;
char addrstr[100];
void *ptr;
int sfd;
std::string parentPortStr = std::to_string(parentPort);
memset (&hints, 0, sizeof (hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
hints.ai_flags = AI_PASSIVE;
cerr << name << " " << parentPortStr << endl;
errcode = getaddrinfo (name.c_str() , parentPortStr.c_str(), &hints, &serverInfo);
if (errcode != 0)
{
cerr << "getaddrinfo has error" << endl;
return;
}
for (rp = serverInfo; rp != NULL; rp = rp->ai_next) {
cerr << "Trying next api " << rp->ai_family << " " << rp->ai_socktype << " " << rp ->ai_protocol << endl;
sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sfd == -1)
continue;
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1){
int enabled = 1;
setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &enabled, sizeof(int));
break;
}
close(sfd);
}
freeaddrinfo(serverInfo);
if(sfd == -1){
cerr << "cannot connect to father" << endl;
return;
}
cerr << "connected to father successfuly. socket: " << sfd << endl;
fatherSocket = sfd;
return;
}当我像这样调用这个代码时: setParent("localhost“,"300");它将始终接受连接。是否有任何程序在端口7300上收听并不重要。
下面是Debug输出:
setparent localhost 300
localhost 7300
Trying next api 2 1 6
connected to father successfully. socket: 5如果我换了港口也没关系。它总是尝试使用ai_family: 2、ai_socktype: 1、ai_protocol: 6的api,并成功地连接到它。
以下是"sudo netstat -tulpn“的结果:
tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN 1163/dnsmasq
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 4814/cupsd
tcp6 0 0 ::1:631 :::* LISTEN 4814/cupsd
udp 0 0 0.0.0.0:45464 0.0.0.0:* 601/avahi-daemon: r
udp 0 0 0.0.0.0:631 0.0.0.0:* 989/cups-browsed
udp 0 0 0.0.0.0:5353 0.0.0.0:* 601/avahi-daemon: r
udp 0 0 0.0.0.0:26517 0.0.0.0:* 5053/dhclient
udp 0 0 127.0.1.1:53 0.0.0.0:* 1163/dnsmasq
udp 0 0 0.0.0.0:68 0.0.0.0:* 5053/dhclient
udp6 0 0 :::50297 :::* 601/avahi-daemon: r
udp6 0 0 :::5353 :::* 601/avahi-daemon: r
udp6 0 0 :::46583 :::* 5053/dhclient正如您所看到的,没有人在端口7300上收听。
我搞不懂那里发生了什么。
发布于 2015-10-25 17:25:55
正如您从自己的netstat显示器中看到的那样,没有人连接到7300。
你测试错了东西。您应该测试enabled是否变成了1。如果sfd失败,connect()不会(不能)神奇地将其设置为-1。
https://stackoverflow.com/questions/33332369
复制相似问题