gpt4 book ai didi

c - Socket : Connect will spend 2 minutes looking for IP before it timesOut. 如何减少那个时间?

转载 作者:行者123 更新时间:2023-12-03 11:51:19 26 4
gpt4 key购买 nike

我想减少 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设置为 EINPROGRESSconnect() 的情况下在没有连接的情况下返回,但仍在尝试做。

请参阅 ERRORS connect()的部分的手册页了解更多信息。

关于c - Socket : Connect will spend 2 minutes looking for IP before it timesOut. 如何减少那个时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10556360/

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