Manually disable networkstream after tcpclient is disabled

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.