本帖最后由 VisualEleven 于 2011-05-05 12:53:19 编辑

解决方案 »

  1.   

    你send是阻塞的还是非阻塞的。在客户端或者服务端关闭都是一样的。
      

  2.   

    tcp保证关闭前把未发完的数据完全发送到对方才断开..如果对方要断开.那么他就会收不到的..
      

  3.   

    待未发送完的数据发送send()出去后再关闭socket,可通过设置: 
      struct linger
     { 
      u_short l_onoff; 
      u_short l_linger; 
      }; 
      linger m_sLinger; 
      m_sLinger.l_onoff = 1; //在调用closesocket()时还有数据未发送完,允许等待 
      // 若m_sLinger.l_onoff=0;则调用closesocket()后强制关闭 
      m_sLinger.l_linger = 5; //设置等待时间为5秒 
      setsockopt( s, SOL_SOCKET, SO_LINGER, ( const char* )&m_sLinger, sizeof( linger ) ); 
      

  4.   

    MSDN:Note  To assure that all data is sent and received on a connection, an application should call shutdown before calling closesocket (see Graceful shutdown, linger options, and socket closure for more information). Also note, an FD_CLOSE network event is not posted after closesocket is called.
    Here is a summary of closesocket behavior:
    If the l_onoff member of the LINGER structure is zero (the default for a socket), closesocket returns immediately and the connection is gracefully closed in the background. 
    If the l_onoff member of the linger structure is set to non-zero and the l_linger member is set to zero (no timeout) closesocket returns immediately and the connection is reset or terminated. 
    If the l_onoff member of the linger structure is set to non-zero and the l_linger member is set to a non-zero timeout:
    – For a blocking socket, closesocket blocks until all data is sent or the timeout expires.– For a nonblocking socket, closesocket returns immediately indicating failure.
      

  5.   

    事实上我已经这么做了,不过看样子似乎是接收乱序了,据说TCP是不会乱序的,但我看了别人的回答,说如果同一个SOCKET有两个线程在接收和发送就可能乱序。-->http://topic.csdn.net/u/20070829/19/d12a5477-d90a-418f-89c7-7d14c980a7f5.html?seed=489971038&r=55793324#r_55793324
    (这是我找到的帖子,里面3楼是这么说的)
      

  6.   

    优雅的关闭你的套接字,或者设置SO_LINGER参数。
      

  7.   

    TCP不会乱序,乱序也是你自己组包乱序