How to Use keep-alive to handle abnormal disconnection of Socket network

Source: Internet
Author: User

Recently, I am responsible for the development of an im project. The server and client are connected over TCP protocol. The server adopts C # development and the client adopts Delphi development. During server development, I encountered various abnormal network disconnections. I have some experience in handling these exceptions. I 'd like to write it out and share it with you.

What are the main causes of network disconnection? To sum up, there are two main types:

1. Client program exception.

In this case, we can handle it well, because the client program will cause a socket exception of connectionreset on the server side (that is, the 10054 exception in Winsock2) when it exits abnormally ). You only need to handle this exception on the server.

2. network link exceptions.

Such as network cable disconnection, switch power loss, and client machine power loss. When these conditions occur, the server will not have any exceptions. In this way, the above Code cannot handle this situation. This is the case in msdn. I will post the original article of msdn here:

If you need to determine the current status of the connection, please do not block, zero-byte send call. If the call is successful or the waewouldblock error code (10035) is triggered, the socket is still in the connection status; otherwise, the socket is no longer in the connection status.

However, in practical applications, I found that this method described by msdn is often ineffective and cannot detect that the network has been disconnected abnormally. What should we do?

We know that TCP has a connection detection mechanism, that is, if no data is transmitted within the specified time (generally 2 hours), a keep-alive datagram will be sent to the peer end, the serial number used is the serial number of the last byte of the last sent packet. If the peer receives the data, it returns a tcp ack to confirm that the byte has been received, the connection is not disconnected. If the response is not received by the other party for a period of time, the system will try again. After several retries, it will send a reset to the peer end and disconnect the connection.

In Windows, the first test is performed every two seconds after the last data is sent. If no response is received for five times, the connection will be disconnected. But two hours is obviously too long for our project. We must shorten this time. So what should we do? I want to use the iocontrol () function of the socket class. Let's take a look at what this function can do:

Use iocontrolcode enumeration to specify the control code and set the low-level operation mode for the socket.

Namespace: system. net. Sockets
Assembly: system (in system. dll)

Syntax

C #
Public int iocontrol (
Iocontrolcode,
Byte [] optioninvalue,
Byte [] optionoutvalue
)

Parameters
Iocontrolcode
An iocontrolcode value that specifies the control code for the operation to be executed.

Optioninvalue
Byte array, which contains the input data required by the operation.

Optionoutvalue
Byte array, which contains the output data returned by the operation.

Return Value
The number of bytes in the optionoutvalue parameter.

For example:

Socket. iocontrol (iocontrolcode. keepalivevalues, inoptionvalues, null );
We need to clarify the definition of inoptionvalues, which is a struct in C ++. Let's take a look at this struct:

Struct tcp_keepalive
...{
U_long Onoff; // whether to enable keep-alive
U_long KeepAliveTime; // how long will the first probe start (unit: milliseconds)
U_long keepaliveinterval; // detection interval (unit: milliseconds)
};

In C #, we directly use a byte array to pass to the function: http://www.devdao.com/

Uint dummy = 0;
Byte [] inoptionvalues = new byte [Marshal. sizeof (dummy) * 3];
Bitconverter. getbytes (uint) 1). copyto (inoptionvalues, 0); // whether to enable keep-alive
Bitconverter. getbytes (uint) 5000). copyto (inoptionvalues, inclual. sizeof (dummy); // how long does it take to start the first probe?
Bitconverter. getbytes (uint) 5000). copyto (inoptionvalues, Marshal. sizeof (dummy) * 2); // detection Interval

Specific implementation code:

Public static void acceptthread ()
...{
Thread. currentthread. isbackground = true;
While (true)
...{
Uint dummy = 0;
Byte [] inoptionvalues = new byte [Marshal. sizeof (dummy) * 3];
Bitconverter. getbytes (uint) 1). copyto (inoptionvalues, 0 );
Bitconverter. getbytes (uint) 5000). copyto (inoptionvalues, Marshal. sizeof (dummy ));
Bitconverter. getbytes (uint) 5000). copyto (inoptionvalues, Marshal. sizeof (dummy) * 2 );
Try
...{
Accept (inoptionvalues );
}
Catch ...{}
}
}

Private Static void accept (byte [] inoptionvalues)
...{
Socket socket = public. s_sockethandler.accept ();
Socket. iocontrol (iocontrolcode. keepalivevalues, inoptionvalues, null );
Userinfo info = new userinfo ();
Info. Socket = socket;
Int id = getuserid ();
Info. Index = ID;
Public. s_userlist.add (ID, Info );
Socket. beginreceive (info. buffer, 0, info. Buffer. length, socketflags. None, new asynccallback (receivecallback), Info );
}

Well, this will be successful.

 

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.