Recently in the maintenance of the company's socket Server tool, the tool is mainly to provide two socket servers service, the two ends connected to the program for transparent forwarding of data.
Program run, encountered a problem, one end of the program is a GPRS device, it is well known that the network connection of GPRS equipment is very not a problem, resulting in a lot of "strange" problems.
In practice, after a few hours of running the program, the wireless end of the socket server disconnects can no longer open. I haven't found it for a long time.
The Wireshark captures the communication message, which is usually a problem when the TCP handshake is three times.
A regular TCP three-time handshake, identified by the TCP identity as simple as: Syn-syn Ack-Ack, is actually encountered when the problem is identified as: Syn-rst ACK.
It can be seen clearly that the server has issued a reset of the identity to actively deny the client's connection.
Part of the program's server code, using the regular TCP asynchronous programming method, is the MSDN code
This server waits for a connection and then uses Asynchronous operations to // accept the connection with initial data sent from the client. // Establish the local endpoint for the socket. iphostentry iphostinfo = Dns.gethostentry (Dns.gethostname ()); ipaddress ipaddress = ipHostInfo.AddressList[0]; IPEndPoint Localendpoint = new ipendpoint (ipaddress, 11000); // Create a TCP/IP socket. Socket listener = new socket (AddressfamiLy. internetwork, sockettype.stream, protocoltype.tcp ); // bind the socket to the local endpoint, and listen for incoming connections. listener. Bind (localendpoint); listener. Listen (; while ) (true) { // set the event to nonsignaled state. alldone.reset (); // Start an asynchronous socket to listen for connections and receive data from the client. console.writeline ("Waiting for a connection ..."); // Accept the connection and receive the first 10 bytes of data. // beginaccept () creates the accepted socket. int receivedDataSize = 10; listener. BeginAccept (Null, receiveddatasize, new asynccallback (acceptreceivedatacallback), listener ); // wait until a Connection is made and processed befoRe continuing. alldone.waitone (); } } public Static void acceptreceivedatacallback (Iasyncresult ar) { // get the socket that handles the client request. Socket listener = ( Socket) ar. asyncstate; // end the operation and display the received data on the console. byte[] Buffer; int bytesTransferred; socket handler = listener. Endaccept (out buffer, out BYTESTRANSFERRED, AR); //receives the socket again, realizes the operation listener. BeginAccept (Null, receiveddatasize, new asynccallback (acceptreceivedatacallback), listener ); }
After the problem is fixed, it can be judged that there is a problem in the asynchronous receiving callback of the program, but after actually adding debugging information, it is found that the port cannot be opened, then the callback function operation is done, and no information is typed.
TCP asynchronous programming, usually is paired to appear beginxxx...endxxx, and then through the callback function to deal with the specific.
The following is the callback function for accept, which uses the try in the code: Catch to catch an exception, the actual problem may be here, the code is as follows:
public static void acceptreceivedatacallback (Iasyncresult ar) { // Get the socket that Handles the client request. socket listener = (Socket) ar. asyncstate; // end the operation and display the received data on the console. byte[] Buffer; int bytesTransferred; try{ socket handler = listener. Endaccept (Out buffer, out bytestransferred, ar); } &Nbsp;catch (Exception 1 e) { ... return; } catch (Exception 2 e) { ... return; } Delivery received again, the implementation has been receiving socket operation listener. BeginAccept (Null, receiveddatasize, new asynccallback (acceptreceivedatacallback), listener ); }
The program has entered "Exception 1"/"Exception 2" before the actual port cannot be opened, judging that it is likely that the program has returned and cannot post the receive operation again.
At this point all port open operations will go into the Socket.listen queue, when the contents of the Accpet queue fail to pass the full begin. The end action is taken out and the underlying protocol stack of the socket after the queue is full rejects the new socket connection.
A problem encountered in asynchronous programming of C # TCP