gpt4 book ai didi

c++ - getaddrinfo 始终连接。即使没有 passive-open 连接监听该端口

转载 作者:行者123 更新时间:2023-11-28 02:18:23 28 4
gpt4 key购买 nike

我正在 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 并不重要。

这是调试输出:

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 上监听。

我不明白那里发生了什么。

最佳答案

正如您从自己的 netstat 显示中看到的那样,也没有人连接到 7300。

你在测试错误的东西。您应该测试 enabled 是否已变为 1。如果失败,connect() 不会(不能)神奇地将 sfd 设置为 -1 .

关于c++ - getaddrinfo 始终连接。即使没有 passive-open 连接监听该端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33332369/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com