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