UDP Punch and Heartbeat package design

Source: Internet
Author: User

First, equipment terminal
Class Deviceclient {
int DeviceID;
int IP;
int port;
Char connectid[16];
time_t Lasttime;
struct Event timeoutev;//Timeout
};
typedef list<deviceclient *> Devicelist;

Second, mobile control terminal
Class Userclient {
int UserID;
int IP;
int port;
Char connectid[16];
time_t lasttime;//When receiving the update, and turn on the timeout device
struct Event timeoutev;//timeout: If timeout, the client is considered to be out of line
};

typedef list<userclient *>userlist;

Third, the UDP heartbeat packet Design (service side)
http://blog.csdn.net/liuguanghui1988/article/details/7090531
3.1 UDP packet Receive thread
1) recvfrom
2) Find Connectid and see if it's a heartbeat pack
3) If it is a heartbeat packet, look for the connectid corresponding client, if the client exists, update the Lasttime, and if the client does not exist, create a new client and update the Lasttime;

3.2 UDP Packet Timeout
1) Definition of time (three consecutive times the heartbeat packet is not received, that is, the time-out is 3 times times the client heartbeat packet time)
2) When the timeout event arrives, the client is considered to be out of line, deleting deviceclient or userclient

Four, UDP heartbeat packet Design (client)
Heartbeat packets are sent every 30 seconds


Five, the design of punching hole
Http://www.cppblog.com/Lee7/archive/2008/01/25/41850.html
Holes are initiated by mobile control terminals or device terminals. The following is an example of mobile control terminal Initiation, detailing the process of punching holes
To facilitate the description, define the mobile control terminal as userclient, the device terminal is deviceclient, the server
The process is as follows:
1) userclient Login
2) Deviceclient Login
3) userclient send "P2p_punch DeviceID" Message to Server
4) Server send "P2p_wantyou UserID" Message to Deviceclient
5) userclient send "P2p_message msg" Message to Deviceclient
6) deviceclient send "P2p_message msg" Message to Userclient

UDP Punch and Heartbeat package design

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.