In the case of a long socket connection, if the server or client has an exception, the other end of the socket dialog operation, the program will be unable to access the disposed object. For example, when the client has a problem to disconnect the socket, when the server operation socket, such as the active disconnection of the socket session, then the program will appear "unable to position the freed objects", because the client causes the server and the client socket is no longer present or the connection has been disconnected, That is, the socket has been released, server re-operation of the server and the client socket will definitely error, so on the server or client side operation socket, you must first determine whether the socket is present, the connection is normal, only the existing and connected to the normal socket to allow operation. Take the server disconnect socket as an example:
Determine if the socket is present and connected properly, and the socket that is connected to it is running for disconnection
if (_clisock! = null && _clisock.connected)
{
Before closing the socket, it is preferred to shutdown the socket on both sides.
_clisock.shutdown (Socketshutdown.both);
Shutdown drop socket After the main thread stop 10ms, to ensure that the socket shutdown complete
System.Threading.Thread.Sleep (10);
Close client sockets, clean up resources
_clisock.close ();
}
Note: The socket dialogue is equivalent to two countries (A and C) through the trade channels for rare earth transactions, normally, a, C countries normally support the trade, trade can proceed normally, if the C Country law prohibits rare earth exports, actively closed the trade channel, but A country does not know, All trade requests from a country to country C will be lost and all maintenance of the trade lanes will fail, so it is necessary to close the trade lanes of the Parties (i.e. close ()) before enforcing the law in State C (i.e. Socketshutdown.both), and then wait for the last batch of goods in the trade channel to successfully ship to country a (i.e., main line hibernation 10ms), then enforce the law and really close the socket connection.
This article quoted: http://blog.sina.com.cn/s/blog_5f4ffa17010112h7.html
A C # Socket connection cannot access a freed object