同步方式中减少断网时connect函数的阻塞时间

本文介绍在网络断开时如何优化connect的阻塞时间,通过使用gethostbyaddr和PING两种方法提前检测到服务主机的路径是否通畅,从而减少不必要的等待时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

同步方式中的断网时connect的阻塞时间为20秒左右,可采用gethostbyaddr事先判断到服务主机的路径是否是通的,或者先ping一下对方主机的IP地址。

A、采用gethostbyaddr阻塞时间不管成功与否为4秒左右。

例子:

ContractedBlock.gifExpandedBlockStart.gifCode
 1 LONG lPort=3024;
 2 
 3 struct sockaddr_in ServerHostAddr;//服务主机地址
 4 
 5 ServerHostAddr.sin_family=AF_INET;
 6 
 7 ServerHostAddr.sin_port=::htons(u_short(lPort));
 8 
 9 ServerHostAddr.sin_addr.s_addr=::inet_addr("192.168.1.3");
10 
11 HOSTENT* pResult=gethostbyaddr((const char *&
12 
13 (ServerHostAddr.sin_addr.s_addr),4,AF_INET);
14 
15 if(NULL==pResult)
16 
17 {
18 
19 int nErrorCode=WSAGetLastError();
20 
21 TRACE("gethostbyaddr errorcode=%d",nErrorCode);
22 
23 }
24 
25 else
26 
27 {
28 
29 TRACE("gethostbyaddr %s"n",pResult->h_name);;
30 
31 


B、采用PING方式时间约2秒左右

转载于:https://www.cnblogs.com/kevinmeng/archive/2009/04/13/1435233.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值