在发送数据包的时候如果启用Nagle算法的话(默认是启用),那么连续发送小的数据时接收方收到的就可能是一个大包,这样的话接收端处理接收的数据就很麻烦,所以我想把Nagle算法禁用。我的代码:BOOL bValue = TRUE;
bValue = psockclt->SetSockOpt(TCP_NODELAY, &bValue, sizeof(BOOL));
TRACE("%d", bValue);bValue返回true但是发送时还是会合并后再发送,这是为什么?

解决方案 »

  1.   

    TCP_NODELAY 
    The TCP_NODELAY option is specific to TCP/IP service providers. The Nagle algorithm is disabled if the TCP_NODELAY option is enabled (and vice versa). 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 TCP/IP service providers enable the Nagle Algorithm by default, and for the vast majority of application protocols the Nagle Algorithm can deliver significant performance enhancements. However, for some applications this algorithm can impede performance, and TCP_NODELAY can be used to turn it off. These are applications where many small messages are sent, and the time delays between the messages are maintained. 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.
      

  2.   

    TCP_NODELAY is the only supported socket option which uses level IPPROTO_TCP; all other options use level SOL_SOCKET.
      

  3.   

    setsockopt(
      s,                 
      IPPROTO_TCP1,   // here             
      TCP_NODELAY,              
      &bValue,
      sizeof(BOOL)                
    );
      

  4.   

    我用的是CAsyncSocket,它的SetSockOpt的level是最后一个参数。
    在调用SetSockOpt时我改成了
    BOOL bValue = TRUE;
    bValue = psockclt->SetSockOpt(TCP_NODELAY, &bValue, sizeof(BOOL), IPPROTO_TCP);
    TRACE("%d", bValue);
    但还是不行,唉,郁闷!!!
      

  5.   

    ???!!!
    ding根据微软的故障修复原则:把程序关闭,然后再次启动程序;如果故障依旧,应该把电脑关闭,然后再次启动电脑!
    哈哈!
    其实我也想知道答案!!
    期盼高手!
      

  6.   

    建议Build一个Release版本来测试一下,如果不行的话,可以考虑在两台机器上测试说不定能有不同。建议到国外网站上搜一下TCP_NODELAY。
      

  7.   

    没听说有关这个SOCK选项的什么情况,你的测试代码是什么样子,能看看吗?
      

  8.   

    我刚编了一个程序就用到这个,没问题,我用TCP协议.
    你得把接收端的Socket缓冲区设成0,否则系统会先把收到的数据放入Socket缓冲区,接收时才拷入你的接收缓冲区.BOOL bValue = TRUE;
    BOOL bRet = psockclt->SetSockOpt(TCP_NODELAY, &bValue, sizeof(BOOL));