Send 和 WSASend

WinSock 常用的API包括TCP协议使用的send、recv、WSASend和WSARecv,UDP协议使用的sendto、recvfrom、WSASendTo 和WSARecvFrom。      

    socket本身有一个send函数,这个函数的只能一次发送一个缓冲区,这对于在发送大量数据的时候或者数据包很多的时候就可能导致系统的低性能,主要原因在于调用太多次的send函数,导致从用户态到核心态的不断切换,而耗费了当前的CPU时钟周期。

    那么解决办法就是减少调用send的次数,一种办法就是使用一个大一点的BUFFER,在发送数据的时候将多个数据包的内容COPY到这个BUFFER中,然后一次发送,这样在一定程度上减少了send的调用次数,但需要一定的编码工作。

    Windows平台上有一个WSASend函数,可以支持一次发送多个BUFFER的请求,每个被发送的数据被填充到WSABUF结构中,然后传递给WSASend函数同时提供BUF的数量,这样WSASend就能上面的工作而减少send的调用次数,来提高了性能。

    实际检验证明,使用WSASend可以提高50%的性能甚至更多。

    PS:禁用Nagle算法的时机:

"Aside from these problems, disabling the Nagle algorithm almost always causes a program's throughput to degrade. The only time you should disable the algorithm is when some other consideration, such as packet timing, is more important than throughput.
Often, programs that deal with real-time user input will disable the Nagle algorithm to achieve the snappiest possible response, at the expense of network bandwidth. Two examples are X Window servers and multiplayer network games. In these cases, it is more important that there be as little delay between packets as possible than it is to conserve network bandwidth."

原文:http://blog.csdn.net/pud_zha/article/details/8033595

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值