1.if recv return 0 then the connection is graceful closed by peer. 2.Please check the value of recemsg before call recv.if the data changed after call recv then you received data already otherwise none data was received.
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.
connection has been gracefully是服务器关了客户的socket还是怎样啊?
2.Please check the value of recemsg before call recv.if the data changed after call recv then you received data already otherwise none data was received.
改成
char recvmsg[1024]
后面两个值再改一下
并设置了so_linger选项
设置或获取 SO_LINGER 选项的值. 其参数为 linger 结构.
struct linger {
int l_onoff; /* 延时状态(打开/关闭) */
int l_linger; /* 延时多长时间 */
};
如果选择此选项, close(2) 或 shutdown(2) 将等到所有套接字里排队的消息成功发送或到达延迟时间后才会返回. 否则, 调用将立即返回. 而 closing 操作将在后台进行. 如果套接字是 exit(2), 的一部分关闭时, 它总是在后台延迟进行的.