Issue 1: Unable to access the disposed object. Object name: "System.Net.Sockets.Socket"
The reason for this scenario is that the socket is called after the socket.close is called somewhere in the program. For this, you can use the Send/sendasync or Receive/receiveasync method to first determine if the socket object has been disposed and the connection is still:
if (stsend! = null && stsend. Connected)
{
Your code ...
}
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.
Question 2. "The remote host forced the shutdown of an existing connection. "
This is generally due to the fact that either the server and the client are actively disconnecting, causing the "remote host to force shutdown of a now connected" exception to appear. This situation causes the program to prompt for an exception and cannot continue execution down.
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.
private void Receiverecordbodycallback (IAsyncResult ar)
{
StateObject state = (stateobject) ar. asyncstate;
Try
{
if (state! = null && state.workSocket.Connected)
{
}
}
catch (Exception e)
{
Asyncsocketserver.closeanddisposeconnection (State, E.tostring (), errorlevel.level1);
}
}
Question 3: Due to the computer's active refusal. Unable to connect.
This is usually the target machine does not turn on the port to listen, such as the server does not start, IP can ping, but connect to the port will be rejected. Check that both the server and client are properly configured for listening ports and connections.
Issue 4. Throws a type System.outofmemoryexeption exception.
One possibility is to turn on the listener in a loop without blocking the thread:
"Acceptclientsasync (Listener, CTS.) Token) "Because it is asynchronous, it returns immediately after the tune-up and then continues the next call, so it is in an infinite loop.
The next connection can be placed in the aceeptcallback of the previous connection:
private void Acceptcallback (IAsyncResult ar)
{
StateObject state = null;
Try
{
Socket listener = (socket) ar. asyncstate;
Socket handler = listener. Endaccept (AR);
Accept the next connection
Listener. BeginAccept (New AsyncCallback (Acceptcallback), listener);
}
}
4. Typically only 1 times per socket () is allowed.
A port has been listening, code is repeatedly executed, and the same port is listening.
Question 5: Your host software aborted an established connection.
This is the server-side shutdown, the exception that the client throws, and the service-side code needs to be diagnosed. It is recommended to deploy a detailed log system on the servers so that problems can be quickly found after the problem.
Issue 6: Long connection problems in asynchronous communication
This is a very common problem, the cause of the network is not smooth caused by delay in receiving or sending data, you can set a heartbeat packet mechanism, that is, when the program is idle at a certain time to send a small packet to the peer, and then to the end of the same packet sent back, to ensure that both sides know that the connection is still there, Otherwise, set the timeout and kick off the peer.
[. NET]-Summary of issues encountered in socket programming