UDP socket programming and methods to improve UDP reliability

 

UDP socket  

 

       Server: ①Create socket(); ②Bind(); ③udp sending and receiving: recvfrom, sendto (the sending and receiving in TCP can be done as a file with read and write; UDP must completely recover the datagram).

 

udp_server.c

        

        

        

 

udp_client.c

       

       

 

        Socket API used in the program are:

       <1>recvfrom

        

       src_addr is an outgoing parameter, the address and port number of the client will be sent out when recvfrom() returns. The addrlen parameter is an incoming and outgoing parameter. What is passed in is the length of the buffer src_addr provided by the caller to avoid buffer overflow problems, and what is passed out is the actual length of the client address structure.

        

       <2>sendto

        

 

        First run the server, then run the client, the client sends a message to the server, and the server receives the message:

        

 

        

       

Improve the reliability of UDP

       The transmission efficiency of UDP is higher than that of TCP, and it has a smaller delay when interacting in the network. For one, the sender only needs to hand over all the application fields to the network layer for processing, and the receiver does not need to confirm the sender to send The sender did not wait for the receiver to confirm the process of the message; second, the TCP header is generally 20 bytes, and the UDP header has only 8 bytes. Therefore, compared with TCP, UDP can be used in IP messages of the same length. Carry more effective application data.
       Many application designers and developers have taken a fancy to this feature of UDP, and use UDP to implement end-to-end data exchange in their applications. However, UDP is not reliable. It does not have TCP confirmation, retransmission, window, and protection. How can UDP-based applications ensure the reliability of their application data in the interactive process? The answer is done by UDP-based applications.
        At present, the following open source programs use UDP to achieve reliable data transmission. They are RUDP, RTP, UDT.
        RUDP
       RUDP provides a set of data service quality enhancement mechanisms, such as congestion control improvement, retransmission mechanism, and desalination server algorithm, etc., so that in the case of packet loss and network congestion, the RTP client (real-time location) is presented with one High-quality RTP streaming. While not interfering with the real-time characteristics of the protocol, the congestion control mechanism of reliable UDP allows flow control behavior in TCP mode.
        RTP
       Real-time Transport Protocol (RTP) provides end-to-end transmission services with real-time characteristics for data, such as interactive video, audio or analog data under multicast or unicast network services. Applications usually run RTP on UDP in order to use its multi-path node and check services; both protocols provide the functions of the transport layer protocol. But RTP can be used with other suitable underlying network or transport protocols. If the underlying network provides a multicast mode, then RTP can use the multicast table to transmit data to multiple destinations.
       RTP itself does not provide a timely delivery mechanism or other quality of service (QoS) guarantees. It relies on the underlying services to implement this process. RTP does not guarantee delivery or prevent out-of-order delivery, nor does it determine the reliability of the underlying network. RTP implements orderly transmission. The sequence number in RTP allows the receiver to reorganize the sender's packet sequence, and the sequence number can also be used to determine the appropriate packet position. For example, in video decoding, sequential decoding is not required.
        UDT
       UDP-based Data Transfer Protocol (UDP-based Data Transfer Protocol, UDT for short) is an Internet data transfer protocol. The main purpose of UDT is to support mass data transmission on high-speed wide area networks, while the standard data transmission protocol TCP on the Internet has poor performance on high-bandwidth and long-distance networks. As the name suggests, UDT is built on UDP and introduces new congestion control and data reliability control mechanisms. UDT is a connection-oriented bidirectional application layer protocol. It supports both reliable data stream transmission and partially reliable datagram transmission. Since UDT is completely implemented on UDP, it can also be used in other application fields besides high-speed data transmission, such as point-to-point technology (P2P), firewall penetration, multimedia data transmission, and so on.

 

 

Guess you like

Origin blog.csdn.net/wxt_hillwill/article/details/73849167