在TCP阻塞模式下怎么才知道recv接收数据已经接收完成?
解决方案 »
- MFC下如何加载文件
- 请教:对话框程序按下回车就会关闭的问题?
- 奇怪的现象:VC6下,F5提示,工程中不包含Debug信息
- 在支持Unicode编程下,CString的Format函数这种格式str.Format(_T("%S-%s")),str1,str2);怎么实现不了?
- 很奇怪的问题
- 在图像处理程序中 一些 WINDIB 类 都是作什么用的
- struct中数组元素调用???????
- 存盘时 ASERT错误!!提示是wincore.cpp的958行的asert.....
- 请教下高手,我写斗地主发牌动画,settimer已经是1了,但是还是很慢,是怎么回事
- 在VC中使用Htmlhelp API函数显示帮助文件,编译时,告知无法辨认Htmlhelp()函数。
- 多线程同步与context switch
- 重新发帖问吧:怎么编程关掉有密码保护的屏幕保护程序?
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.
下一次接收时收到A2+B。这时候需要一种机制,把分离的数据包合并(A1)第一次+(A2+B)第二次,并且能正确的把
A、B拆分开来(A1+A2),B。
接收成功返回值表示收到的字节数,根据这个数你可以判断是否收到了所有数据,
不过你首先要有协议告诉接收端该数据的长度
stream socket本身不是面向消息的,而是面向字节流的,所以逻辑上的消息边界需要你自己定义。
socket,系统不会提供你任何“完成一次接收”得信息得要做到分包,有两种方法
方法1:发送方首先发送一个长度信息,事先规定好这个信息2字节或者4字节,得到这个长度,你就知道怎么收了
方法2:规定一个报文结束符,这个结束符必须不在正文中出现。一般用于文本传输,例如,中间如果收到一个\0就认为结束了注意:系统不会提供任何方法告诉你接收完了,这需要你发送和接收双方在报文内协商好
所以,你得对你的数据进行处理
也可以像八楼那样,先发送你的文件长度过去,再以每次接受到的长度累加起来与首先发送过来的总长度相比较,就可以知道有没有完成了.
int len=recv(...);
if (len<=0)
没接收倒数据
else
接收到数据
不用那么麻烦吧,
如果是大数据分批传送,在主客程序传送的时间加结尾标识也可以判断一下