1、快速通信
Winsock的Nagle算法将降低小数据报的发送速度,而系统默认是使用Nagle算法,使用
| int setsockopt( SOCKET s, int level, int optname, const char FAR *optval, int optlen );函数关闭它 |
例子:
| SOCKET sConnect; sConnect=::socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); int bNodelay = 1; int err; err = setsockopt( sConnect, IPPROTO_TCP, TCP_NODELAY, (char *)&bNodelay, sizoeof(bNodelay));//不采用延时算法 if (err != NO_ERROR) TRACE ("setsockopt failed for some reason\n");; |
2、SOCKET的SegMentSize和收发缓冲
TCPSegMentSize是发送接受时单个数据报的最大长度,系统默认为1460,收发缓冲大小为8192。
在工控系统中,建议关闭Nagle算法,每次发送数据小于1460个字节(推荐1400),这样每次发送的是一个完整的数据报,减少对方对数据流的断帧处理。
3、同步方式中减少断网时connect函数的阻塞时间
同步方式中的断网时connect的阻塞时间为20秒左右,可采用gethostbyaddr事先判断到服务主机的路径是否是通的,或者先ping一下对方主机的IP地址。
A、采用gethostbyaddr阻塞时间不管成功与否为4秒左右。
例子:
| LONG lPort=3024; struct sockaddr_in ServerHostAddr;//服务主机地址 ServerHostAddr.sin_family=AF_INET; ServerHostAddr.sin_port=::htons(u_short(lPort)); ServerHostAddr.sin_addr.s_addr=::inet_addr("192.168.1.3"); HOSTENT* pResult=gethostbyaddr((const char *) & (ServerHostAddr.sin_addr.s_addr),4,AF_INET); if(NULL==pResult) { int nErrorCode=WSAGetLastError(); TRACE("gethostbyaddr errorcode=%d",nErrorCode); } else { TRACE("gethostbyaddr %s\n",pResult->h_name);; } |
B、采用PING方式时间约2秒左右
暂略