TCP heartbeat Mechanism

Source: Internet
Author: User

When connected to the server, you can perform other operations to prevent frequent access to the client or frequent access to the client, but the server does not know, get a half answer to this situation.

Of course, you can add a heartbeat packet in the suggestion, and then the server will renew the package at a certain time, check which response exceeds the time limit and no response is made. When the time exceeds the limit. However, this is a bit uncomfortable. It should be done on your own behalf. You also need to set the sequence list, which is quite large.

I remember that the keepalive of TCP can be used as the keepalive token on the Internet, but it is also said that it has no effect. I want to decide whether this person in the TCP protocol will not set some useless functions, but nothing has been seen on msdn. Why does Ms actually show them? I decided to renew it.

1. Delphi's winsock2.pas is incomplete and some things need to be added, as shown below:

Const
Ioc_in = 80000000 h; // there is a question about the blog in the 80000000 hexadecimal format.
Ioc_vendor = 18000000 h; // there is a question about the blog in the 80000000 hexadecimal format.
Ioc_out = 40000000 h; // there is a question about the blog in the 80000000 hexadecimal format.
Sio_keepalive_vals = ioc_in or ioc_vendor or 4;

Type
Ttcp_keepalive = packed record
Onoff: integer;
KeepAliveTime: integer;
Keepaliveinterval: integer;
End;

2. When the accept receives a new token, set its retention time as follows:

// Set keepalive to enable the zookeeper Mechanism
OPT: = 1;
If setsockopt (hclient, sol_socket, so_keepalive, @ opt, sizeof (OPT) <> 0 then
Begin
Outputdebugstring ('setsockopt keepalive Error !!! ');
End;

// Keepalive time set the retention time
Klive. Onoff: = 1; // your warranty
Klive. KeepAliveTime: = 10000; // indicates the expiration time.
Klive. keepaliveinterval: = 1; // timeout value
If wsaioctl (hclient, sio_keepalive_vals,
@ Klive,
Sizeof (ttcp_keepalive ),
@ Outklive,
Sizeof (ttcp_keepalive ),
@ Opt,
0, nil) = socket_error then
Begin
Outputdebugstring ('wsaioctl keepalive error ');
End;

 

 

 

========================================================== ====================

 

Anyone who has experienced network application development knows that receiving and sending data on the network are implemented using socket in windows. However, if the socket is disconnected, the data sending and receiving operations may fail. But how can I determine whether this socket can be used?

Some people must think of using the returned results in the send function for judgment. If the returned length is the same as the length sent by the user, it indicates that the socket is available. Otherwise, the socket must be faulty. But we are not sending data all the time. How can this problem be solved?

In fact, TCP has implemented a heartbeat mechanism for us. If you set a heartbeat, TCP will send the heartbeat (for example, twice) of the number of times you set within a certain period of time (for example, 3 seconds ), this information does not affect your own protocols.

There are many examples of implementing heartbeat in VC, but there is no corresponding code in dlephi. The following is the heartbeat code I wrote using Delphi (taking iocp as an example). I hope it will help you.

Define heartbeat Constants

Const
Ioc_in =$ 80000000;
Ioc_vendor = $18000000;
Ioc_out = $40000000;
Sio_keepalive_vals = ioc_in or ioc_vendor or 4;

VaR

Inkeepalive, outkeepalive: ttcp_keepalive;

The implementation code is added after acceptsc: = wsaaccept (listensc, nil, 0:

OPT: = 1;
If setsockopt (acceptsc, sol_socket, so_keepalive, @ opt, sizeof (OPT) = socket_error then
Begin
Closesocket (acceptsc );
End;
Inkeepalive. Onoff: = 1;
// Set the 3-second interval

Inkeepalive. KeepAliveTime: = 3000;

// Set the heartbeat sent once every 3 seconds
Inkeepalive. keepaliveinterval: = 1;
Insize: = sizeof (ttcp_keepalive );
Outsize: = sizeof (ttcp_keepalive );
If wsaioctl (accept, sio_keepalive_vals, @ inkeepalive, insize, @ outkeepalive, outsize, @ outbyte, nil, nil) = socket_error then
Begin
Closesocket (acceptsc );
End;

If the above Code is added, the system adds a heartbeat every 3 seconds. If the client is disconnected (the network cable is disconnected), The getqueuedcompletionstatus function returns false.

If (getqueuedcompletionstatus (completionport, bytestransferred, DWORD (perhandledata), poverlapped (periodata), infinite) = false) then
Begin
// Handle client disconnection information here.

Continue;
End;

The above is my Method of Using Heartbeat, which I have used in my online games. Stability!

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.