C#socket Communication two most classic error solutions

Source: Internet
Author: User

1, the classic error can not access the freed objects. Object name: "System.Net.Sockets.Socket"
(1), Problem scene

(2), problem narration

Somewhere in the program called after the Socket.close, the socket is also called, the above error!

(3). Solution

Use the following sentence:


if (stsend! = null && stsend. Connected)

Stsend! = null: This sentence will be invalid after socket.close, because all of its connected resources have been released when the socket connection is closed, so it should be used with connected;

Stsend. Connected: Gets a value that indicates whether the last operation of the socket was send or receive as a connection to the remote host, and if not, it was disconnected.

Extension Description:

All unmanaged resource programmers must be able to control the release of resources, such as database connections, socket connections, etc. should be shown off after use, if it is a long connection, you should ensure that all occupied connections are closed when the program exits.

2. Classic error "The remote host forced the shutdown of an existing connection. "

(1) Problem site

(2), problem narration

For example, the classic problem with the network, because it is the server and the client of either side of the active disconnection, causing the "remote host forced to shut down a now connected" exception. This situation causes the program to prompt for an exception and cannot continue execution down.

(3). Solution

By exception handling, through the exception to indicate that the network is not normal, and thus jump out of the program branch, not because of abnormal problem program paralysis!

Such as A catch to this kind of System.Net.Sockets.SocketException exception (of course, it is set to Exceptioin exception, grab a wider range), then use the MessageBox to give the user a hint, and then exit from the branch on the line.

This article is quoted from: http://blog.chinaunix.net/uid-25498312-id-3321852.html

C#socket Communication two most classic error solutions

Related Article

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.