We know that UDP is defined as an unreliable protocol. Even so, in some applications, UDP protocol also has a huge advantage over TCP protocol. Next, let's discuss the relevant content. UDP is an unreliable network protocol. What is the value or necessity of UDP? In fact, in some cases, UDP may become very useful. Because UDP has a speed advantage beyond the reach of TCP. Although various security protection functions are embedded in the TCP protocol, a large amount of system overhead will be occupied during actual execution, and the speed will undoubtedly be seriously affected. In contrast, UDP eliminates the information reliable transfer mechanism and transfers security and sorting functions to upper-layer applications, greatly reducing the execution time and ensuring the speed.
The earliest specification for UDP protocol was RFC768, which was released in 1980. Although it has been a long time, UDP continues to play a role in mainstream applications. Many applications, including video teleconference systems, prove the value of UDP. Compared with reliability, these applications focus more on actual performance. Therefore, in order to achieve better performance, for example, higher image frame refresh rate, certain reliability such as meeting quality can be sacrificed ). This is the trade-off between UDP and TCP. Based on different environments and features, the two transmission protocols will play a more important role in the future online world.
UDP Server program
1. Write the UDP Server program
1) Use socket) to establish a UDP socket. The second parameter is SOCK_DGRAM.
2) initialize the variables in the sockaddr_in structure and assign values. Sockaddr_in structure definition:
- struct sockaddr_in {
- uint8_t sin_len;
- sa_family_t sin_family;
- in_port_t sin_port;
- struct in_addr sin_addr;
- char sin_zero[8];
- };
Here, "08" is used as the port of the service program, and "INADDR_ANY" is used as the bound IP address, that is, the address on any host.
3) bind the socket and the defined IP address and port. Check whether the bind is successfully executed. If there is an error, exit. This prevents repeated running of service programs.
4) enter the infinite loop program and use recvfrom) to enter the waiting state until the customer program receives the data sent, it processes the received data and sends feedback to the customer program. Here, the received data is directly sent back to the customer program.
2. udpserv. c program content:
- #include
- #include
- #include
- #include
- #include
- #include
- #define MAXLINE 80
- #define SERV_PORT 8888
- void do_echoint sockfd, struct sockaddr *pcliaddr, socklen_t clilen)
-
- {
- int n;
- socklen_t len;
- char mesg[MAXLINE];
- for;;)
- {
- len = clilen;/* waiting for receive data */
- n = recvfromsockfd, mesg, MAXLINE, 0, pcliaddr, &len);/* sent data back to client */
- sendtosockfd, mesg, n, 0, pcliaddr, len);
- }
- }
-
- int mainvoid)
- {
- int sockfd;
- struct sockaddr_in servaddr, cliaddr;
- sockfd = socketAF_INET, SOCK_DGRAM, 0); /* create a socket */
- /* init servaddr */
- bzero&servaddr, sizeofservaddr));
- servaddr.sin_family = AF_INET;
- servaddr.sin_addr.s_addr = htonlINADDR_ANY);
- servaddr.sin_port = htonsSERV_PORT);
- /* bind address and port to socket */
- ifbindsockfd, struct sockaddr *)&servaddr, sizeofservaddr)) == -1)
- {
- perror"bind error");
- exit1);
- }
- do_echosockfd, struct sockaddr *)&cliaddr, sizeofcliaddr));
- return 0;
- }
At this point, we will briefly introduce some UDP application programs. In subsequent articles, we will analyze the content of the UDP Client program.