Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A problem encountered in C # TCP Asynchronous programming

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Recently, it is maintaining a socket server tool of the company, which mainly provides two socket server services to transparently forward data to programs connected at both ends.

During the operation of the program, there is a problem. One end of the program is the GPRS device. As we all know, the network connection of the GPRS device is not a problem, which will lead to a lot of "strange" problems.

In practice, after a few hours of running the program, the socket server on the wireless side is disconnected and can no longer be opened. I looked for it for a long time, but I didn't find it.

The problem of grabbing communication messages through wireshark is usually caused by the three-way handshake of TCP.

The regular TCP three-way handshake can be simply regarded as SYN-SYN ACK-ACK by the logo of TCP. When a problem is actually encountered, it is marked as: SYN-RST ACK.

It is obvious that the server issued a reset identity to actively reject the client's connection.

The server part of the program code, using the conventional TCP asynchronous programming method, the following 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 (100); 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); / / send and receive again to realize the operation listener.BeginAccept (null, receivedDataSize, new AsyncCallback (AcceptReceiveDataCallback), listener) of receiving socket all the time;}

After the positioning of the problem, it can be judged that there may be a problem in the asynchronous receiving callback of the program, but after actually adding debugging information, it is found that after the port of the program cannot be opened, the callback function operation is carried out, and there is no message.

TCP asynchronous programming, usually appears in pairs of beginXXX...endXXX, and then through the callback function to deal with.

The following is the callback function of accept. Try..catch is used to catch exceptions in the code. The actual problem may lie 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);} catch (exception 1 e) {... Return;} catch (exception 2e) {... Return;} / / send and receive again to realize the operation listener.BeginAccept (null, receivedDataSize, new AsyncCallback (AcceptReceiveDataCallback), listener) that has been receiving socket;}

The program has entered "exception 1" / "exception 2" before the actual port cannot be opened, and it is likely that the program has carried out return, but cannot deliver and receive the operation again.

At this time, all port opening operations will enter the queue of socket.listen (backlog). When the contents of the accpet queue cannot be fetched through the full begin..end operation, the underlying protocol stack of socket will reject the new socket connection when the queue is full.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report