If the client disconnects the connection gracefully by calling closesocket or shutdown API, the onclose event will be activated. However, if the network disconnectes because of physical problem, for example, network line and OS shuts down, the onclose will not be notified.
Therefore, there is no method to detect the physical disconnection of a network. There is also no method to detect the program down of the opposite side.
However there are 2 ways.
The first way: you need to set SO_KEEPALIVE by setsockopt API.
Please refer to the following article:
Q140325 How to Determine Loss of Client/Server Connection
http://support.microsoft.com/support/kb/articles/q140/3/25.asp
The other way: you can sets a socket as a non-block in the program, and implement TIMEOUT by select(). If the opposite side does not respond for a certain time, we should assume the opposite side is down and close by close().