A client I recently wroteProgramIf a problem occurs, that is, when many users are offline at the same time (for example, unplugging the network cable from a computer on many clients), the server may have an exception at a very low probability. Abnormal trace shows that this is caused by writing data to closed tcpclient. Specifically, when networkstream. beginwrite method is called, invalidoperationexception occurs. The message is "write operation not supported ". This exception did not even appear in the msdn document of beginwrite.
I checked it.CodeThe main reason is that many users are disconnected at the same time, and the heartbeat may stop at the same time. When a user exits, a notification message is sent to all other users. At this time, if a user sends a message to other users upon exit, and the user exits, the tcpclient has been disabled, and a problem occurs when the user sends a message to the networkstream: an exception occurs. (The reason for the low probability is that everything happens between several calls)
Solution: When tcpclient is disabled, manually disable the networkstream obtained from it (call the close method ). In this case, if there are still calls, objectdisposedexception will occur, and this exception is captured. Therefore, the change to solve this problem is to add m_stream.close () in the underlying public communication library (). Everything is normal!
Later, let's look at msdn. Microsoft also said that networkstream must be disabled along with tcpclient. Disabling tcpclient does not mean that networkstream is also disabled. Write C # network program as a warning.