我想减少 socket:connect 查找要连接的 IP/端口所需的允许超时时间?在某些使用 IP 10.0.0.x 的网络路由器(例如 Netgear)上,只需不到一秒就会超时。
注意:“选择”稍后出现
host = gethostbyname("xxx");//invalid IP,
memcpy(&(sin.sin_addr), host->h_addr, host->h_length);
sin.sin_family = host->h_addrtype;
sin.sin_port = htons(4000);
s = socket(AF_INET, SOCK_STREAM, 0);
hConnect = connect(s, (struct sockaddr*)&sin, sizeof(sin));//sits here for 2 minutes before moving on to the next line of code
bConn = 0;// no connect
if (hConnect == 0) {
bConn = 1;// connect made
}
谢谢
最佳答案
在调用 connect()
之前将套接字设置为非阻塞,然后执行 select()
或 poll()
它可以找出正在发生的任何事件。
注意:使用此设置,您将从 connect()
获得非零返回,并且 errno
设置为 EINPROGRESS
connect()
的情况在没有连接的情况下返回,但仍在尝试执行。
有关详细信息,请参阅 connect()
手册页的 ERRORS
部分。
关于c - Socket : Connect will spend 2 minutes looking for IP before it timesOut. 如何减少该时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10556360/