Using UDP to complete network communication

Source: Internet
Author: User

Language chat has the ability to accept drops but cannot accept the disorder, so you can use UDP to

Transfer data for increased efficiency.

Because of the unreliable nature of UDP transmission, in order to ensure the player's reliable access to the server and some

The correct execution of the operation, or the need for some additional code to ensure the reliability of UDP transmission.

A simple solution is to wait for the Recvack sent by the receiving end after sending a packet on the sending side.

If the recvack is not received, it is sent repeatedly until the timeout expires. Send end to Recvack send

Confrimack. The receiving end knows the timeout if it does not receive a confrimack duplicate send.

To differentiate between reliable and non-reliable transmissions, packages are provided with different package types in the form of enumerations

Need to provide different:

enum Sendtype {

Safe_send,

Unsafe_send,

and a different sending interface is available:

void unsafe_send (char* buf,int len,const sockaddr* addr, int addrlen) {

int datalen + = Sizeofsenddatahead;

SendData * data = (senddata*) malloc (datalen);

Data->type = Unsafe_send;

Data->len = Len;

memcpy (Data->data, buf, Len);

SendTo (FD, data, Datalan, 0, addr, addrlen);

}

void Safe_send (char* buf,int len,const sockaddr* addr, int Addrlen) {

int datalen + = Sizeofsenddatahead;

SendData * data = (senddata*) malloc (datalen);

Data->type = Safe_send;

If the receiving end receives the data, if it is safe_send, the recvack is sent:

void Sendrecvack (sockaddr_in & addr, int addrlen) {
Recvack cmd;
CMD.ISRECV = Cmdrecvack;
Safe_send (&cmd, Sizeofconfrimack, (sockaddr*) &addr, Addrlen);

The sender sends the Confrimack after receiving the Recvack:

void sendconfrimack (sockaddr_in & addr, int addrlen) {

confrimack cmd;

CMD.ISRECV = Cmdconfrimack ;

unsafe_send (&cmd, Sizeofconfrimack, (sockaddr*) &addr, Addrlen);

Use the heartbeat control to re-bundle:

void safesendheadbeat (int time, void * data) {

if (!vsenddata.empty ()) {

senddata * data = Vsenddata.front ();

SendTo (FD, data, Data->len, 0, addr, addrlen);

}

Addtime (safesendheadbeat, data);

Using the heartbeat control Recvack:

void recvackheadbeat (int time , void * data) {

if (Recvack! = 0) {

SendTo (FD, Recvack, Recvack->len, 0, addr, addrlen);

}

Addtime (Recvackheadbeat, recvack);

Using UDP to complete network communication

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.