2
服务器开发中我们经常会处理粘包的问题,而在处理粘包的时候,有时候为了简化包,就需要使得客户端设置Nagle算法,避免粘包出现。而这个相关信息就是TCP_NODELAY的问题。看看MSDN中的提示:
TCP_NODELAY的出现是为了避免频繁的发包导致服务器压力过大,就将几个小包按照TCP流的模式,拼起来一起发送,这样也导致了服务器在处理的时候需要将这几个包拆开来处理,无疑加大了服务器端处理逻辑的繁杂。当然这个的设置会提高TCP的性能,但是为了简化服务器的逻辑的情况,可以考虑避免他们延迟发送,而出现包就直接发送而不等待。



IPPROTO_TCP
TCP_NODELAY
both
BOOL
Enables or disables the Nagle algorithm for TCP sockets. Enabled by default. The Nagle algorithm is disabled if the TCP_NODELAY option is enabled, and the reverse is true as well.

Note:  
Application writers should not set TCP_NODELAY unless the impact of doing so is well understood and desired because setting TCP_NODELAY can have a significant negative impact on network and application performance.  

The Nagle algorithm (described in RFC 896) is effective in reducing the number of small packets sent by a host. The process involves buffering send data when there is unacknowledged data already in flight, or buffering send data until a full-size packet can be sent. It is highly recommended that Windows Sockets implementations enable the Nagle Algorithm by default because the algorithm can deliver significant performance enhancements. However, for applications where many small messages are sent, and the time delays between the messages are maintained, this algorithm can impede performance. In this case, setsockopt with TCP_NODELAY can be used to turn it off.

Tags: , | 引用(0)
qq:274145322
2008/12/13 13:44
我也碰到过,你在每个包前面加一个包长度即可,因为TCP是流,不可能丢失数据,所以每次先收4个字节,再收包。测试情况良好。
huzhangyou2002 回复于 2008/12/13 14:50
方法不错,可以借鉴一下。
the sea
2008/12/08 16:24
SetSockOpt(hSocket,IPPROTO_TCP,TCP_NODELAY,PChar(@optval),SizeOf(optval))我做了测试。不停地发1-1000还是会粘包
分页: 1/1 第一页 1 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]