Why you choose the server to send the keepalive packets? In most cases the client is interested in keeping a connection open.
You did not tell us about the used protocol and the typical data flow. Depending on that it might help to define a disconnect message.
It might be even not necessary to use keepalive. See also
http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html[
^].
"TCP Spurious Retransmission"
Is this a Wireshark message? Then they might be repeated SYN packets. See also
Spurious Retransmissions | Packet Foo | Analyzing network packets since 2003[
^].
I suggest to rethink your setup. If we should help you should tell us about the data flow, when and why connections should be closed programmatically and how closed connections should be detected on both sides (keepalive, time outs, sending fails).
[EDIT: For updated question]
Quote:
After deeper investigation, the error code of closesocket is mainly case WSAENOTSOCK: "The descriptor is not a socket." I don't know what cause this error code.
The most common reason for this return code is that the socket has already been closed (assuming that the socket has been successfully created). If you call for example
closesocket
for a valid socket two times in a row, the second call will fail with this error code. Because sockets are reused by the system, this behaviour is not guaranteed (especially if some time has elapsed).
A common method to avoid duplicate calls is assigning
INVALID_SOCKET
after a socket has been closed and checking for this value before trying to close it.
I suggest to post some code snippets to get further help. Please try to post only necessary code (which might be difficult because such errors require a clear view on what is going on).
[/EDIT]