关于closesocket会不会等待数据发送完毕
MSDN上说The closesocket function closes a socket. Use it to release the socket descriptor s so that further references to s fail with the error WSAENOTSOCK. If this is the last reference to an underlying socket, the associated naming information and queued data are discarded. Any pending blocking, asynchronous calls issued by any thread in this process are canceled without posting any notification messages.
An application should not assume that any outstanding I/O operations on a socket will all be guaranteed to completed when closesocket returns. The closesocket function will initiate cancellation on the outstanding I/O operations, but that does not mean that an application will receive I/O completion for these I/O operations by the time the closesocket function returns. Thus, an application should not cleanup any resources ( WSAOVERLAPPED structures, for example) referenced by the outstanding I/O requests until the I/O requests are indeed completed.
上面好像是说调用closesocket后任何排队的数据都会被丢弃。
那么我在阻塞模式下,
1.等send函数返回(说明数据已经复制到发送缓冲区)后再立即调用closesocket,closesocket会等待发送缓冲区的数据发送完吗?还是直接消耗缓冲区?
2.我在send阻塞时,调用closesocket,它会取消send函数的调用对吗?
我看《UNIX网络编程》中说close调用后,不能再在这个套接字描述符上调用其他套接字函数,TCP会尝试发送缓冲区中剩余的数据。发送完后紧接着是TCP终止序列。winsock的closesocket是一样的吗?
这是不是要把closesocket和TCP的行为分开啊!
[解决办法]
可以设置linger 为false或者true来定义closesocket的行为
[解决办法]
SO_LINGER选项为系统设定在关闭套接字(closesocket)以后但在放弃连接之前等待的最长时间值,通过linger结构维护着套接字有关当数据在套接字上排队等候发送时或在调用closesocket时应采取的动作
typedef struct linger
{
u_short l_onoff,
u_short l_linger
}
将l_linger设置为0或者需要以秒为单位的超时值(l_onoff=0).
[解决办法]
设置linger 去改变closesocket