在TCP阻塞模式下怎么才知道recv接收数据已经接收完成?

解决方案 »

  1.   

    看不明白,那个证明已经接收完。  对方关闭的话返回0,SOCKET_ERROR错误中得回到的值是那个才表示接收完?晕了...Return Values
    If no error occurs, recv returns the number of bytes received. If the connection has been gracefully closed, the return value is zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.Error code Meaning 
    WSANOTINITIALISED A successful WSAStartup call must occur before using this function. 
    WSAENETDOWN The network subsystem has failed. 
    WSAEFAULT The buf parameter is not completely contained in a valid part of the user address space. 
    WSAENOTCONN The socket is not connected. 
    WSAEINTR The (blocking) call was canceled through WSACancelBlockingCall. 
    WSAEINPROGRESS A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function. 
    WSAENETRESET The connection has been broken due to the keep-alive activity detecting a failure while the operation was in progress. 
    WSAENOTSOCK The descriptor is not a socket. 
    WSAEOPNOTSUPP MSG_OOB was specified, but the socket is not stream-style such as type SOCK_STREAM, OOB data is not supported in the communication domain associated with this socket, or the socket is unidirectional and supports only send operations. 
    WSAESHUTDOWN The socket has been shut down; it is not possible to receive on a socket after shutdown has been invoked with how set to SD_RECEIVE or SD_BOTH. 
    WSAEWOULDBLOCK The socket is ed as nonblocking and the receive operation would block. 
    WSAEMSGSIZE The message was too large to fit into the specified buffer and was truncated. 
    WSAEINVAL The socket has not been bound with bind, or an unknown flag was specified, or MSG_OOB was specified for a socket with SO_OOBINLINE enabled or (for byte stream sockets only) len was zero or negative. 
    WSAECONNABORTED The virtual circuit was terminated due to a time-out or other failure. The application should close the socket as it is no longer usable. 
    WSAETIMEDOUT The connection has been dropped because of a network failure or because the peer system failed to respond. 
    WSAECONNRESET The virtual circuit was reset by the remote side executing a hard or abortive close. The application should close the socket as it is no longer usable. On a UPD-datagram socket this error would indicate that a previous send operation resulted in an ICMP "Port Unreachable" message. 
      

  2.   

    在某些情况下数据包会被拆解,例如:线上有A数据包(由A1+A2组成),和B数据包。那么在客户端有可能先收到A1,
    下一次接收时收到A2+B。这时候需要一种机制,把分离的数据包合并(A1)第一次+(A2+B)第二次,并且能正确的把
    A、B拆分开来(A1+A2),B。
      

  3.   

    If no error occurs, recv returns the number of bytes received.
    接收成功返回值表示收到的字节数,根据这个数你可以判断是否收到了所有数据,
    不过你首先要有协议告诉接收端该数据的长度
      

  4.   

    总归需要应用层协议吧。
    stream socket本身不是面向消息的,而是面向字节流的,所以逻辑上的消息边界需要你自己定义。
      

  5.   

    tcp在发送数据的时候会记录数据长度.当接受结束的时候recv会返回.tcp协议里面在 包头部分 有对应的记录.这里不需要你担心.
      

  6.   

    recv 会返回每次实际接收到的长度,你累计每次收到的长度,和要接收的比较就知道有没有接收完了。
      

  7.   

    TCP协议没有定义所谓“完了”这种东西,他就如同河流一样,是源源不断得发得。除非你关闭
    socket,系统不会提供你任何“完成一次接收”得信息得要做到分包,有两种方法
    方法1:发送方首先发送一个长度信息,事先规定好这个信息2字节或者4字节,得到这个长度,你就知道怎么收了
    方法2:规定一个报文结束符,这个结束符必须不在正文中出现。一般用于文本传输,例如,中间如果收到一个\0就认为结束了注意:系统不会提供任何方法告诉你接收完了,这需要你发送和接收双方在报文内协商好
      

  8.   

    tcp只负责顺序的接收数据,数据由发送方提供,发送一次接收一次,至于出现发送N次接收一次的情况,在以太网中一个TCP的数据包长度是1500位。其中20位的IP包头,20位的TCP包头,其余的1460都是我们可以发送的数据。在数据发送的时候,我们发送的数据长度有可能比1460短,这样在TCP来说它还是以一个数据包来发送。从而降低了网络的利用率。所以TCP在发送数据包的时候,会将下一个数据包和这个数据包合在一起发送以增加网络利用率(虽然SOCKET 中可以强制关闭这种合并发送,但是不建议使用)。
    所以,你得对你的数据进行处理
      

  9.   

    冠西,看着这么长的帖子没结的而且分那么底的在 csdn上估计就是你的了,加分吧.不然小心再散你艳照..
      

  10.   

    If no error occurs, recv returns the number of bytes received. If the connection has been gracefully closed, the return value is zero. 当recv返回值为0时就证明对方发送完毕断开了连接,接受完成了(不考虑意外断开).
    也可以像八楼那样,先发送你的文件长度过去,再以每次接受到的长度累加起来与首先发送过来的总长度相比较,就可以知道有没有完成了.
      

  11.   

    recv函数的返回值是接收到的字符长度,
    int len=recv(...);
    if (len<=0)
       没接收倒数据
    else
       接收到数据
    不用那么麻烦吧,
    如果是大数据分批传送,在主客程序传送的时间加结尾标识也可以判断一下
      

  12.   

    recv完成后会返回接受到的字节的长度