Network programming in embedded linux (4) -- UDPServer programming the design of TCP-based communication programs is introduced earlier. the TCP protocol implements the connection, reliability, and transmission control protocol for transmitting data streams, however, UDP is non-connected and unreliable. Because UDP does not provide reliability
Network programming in embedded linux (4) -- UDP Server programming
We have introduced the design of TCP-based communication program. The TCP protocol implements the connection, reliable, and transmission control protocol for transmitting data streams, while UDP is non-connected and unreliable, the transmission protocol used to transmit data packets. because UDP does not provide reliability, it has less transmission latency. Therefore, UDP is often used in scenarios with high speed requirements.
1. UDP communication process
The basic process of UDP communication is as follows: on the server side, the server first creates a socket of the UDP datagram type. the socket type is SOCK_DGRAM, and then the server side calls the bind function, bind a port to a UDP socket. because no connection is required, the server can call the recvfrom function to wait for the UDP datagram sent by the client at the specified port. on the client, you also need to create a datagram socket through the socket function, and then assign the port number to the socket by the operating system. then the client can use the sendto function to send a UDP datagram to an address. after the server receives the data, it returns it from recvfrom. after processing the data, it calls the sendto function to return the processing result to the client. shows the communication process of a UDP connection:
It can be seen that the communication process of UDP connection is much simpler than that of TCP connection. because the UDP server process does not need to receive new connections from the listening socket as the TCP server, instead, it only needs to wait for the datagram sent from the client on the bound port, therefore, UDP servers usually work cyclically.
Another difference is that a TCP server is monopolized by a client after the client establishes a connection. to achieve this, you must provide services for multiple clients at the same time, multiple server sub-threads or sub-processes are required. the UDP server establishes connections with different clients, so the clients do not exclusively occupy the UDP server. for example, the DNS server uses the UDP protocol. after the server processes the datagram sent from a client, it can understand how to process the datagram from another client, many time-consuming connection establishment and destruction processes are omitted. improves the server processing capacity.
2. UDP communication server
Next we will look at an example of a UDP server program. The next article will explain how to write the client program.
/*************************************** **************************************** * ****** // * Introduction: UDPServer example. *//************************************* **************************************** * *******/# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include # define PORT 2000/* listener PORT */# define MAXDATASIZE 100/* buffer size */# define STR "Welcome to my server. \ n "int main () {int sockfd;/* server address information */struct sockaddr_in server;/* client address information */struct sockaddr_in client; int sin_size; int num;/* receive buffer */char msg [MAXDATASIZE];/* create UDP socket */if (sockfd = socket (AF_INET, SOCK_DGRAM, 0 )) =-1) {perror ("Creating socket failed. "); exit (1);} bzero (& server, s Izeof (server); server. sin_family = AF_INET; server. sin_port = htons (PORT); server. sin_addr.s_addr = htonl (INADDR_ANY); if (bind (sockfd, (struct sockaddr *) & server, \ sizeof (struct sockaddr) =-1) {/* handle exception */perror ("Bind error. "); exit (1);} sin_size = sizeof (struct sockaddr_in); while (1) {num = recvfrom (sockfd, msg, MAXDATASIZE, 0, \ (struct sockaddr *) & client, & sin_size); if (num <0) {perror (" Recvfrom error \ n "); exit (1);} msg [num] = '\ 0'; printf (" You got a message (% s) from % s \ n ", msg, \ inet_ntoa (client. sin_addr);/* send messages to the client */sendto (sockfd, STR, strlen (STR), 0, \ (struct sockaddr *) & client, sin_size ); /* if the message is quit, exit the server program */if (! Strcmp (msg, "quit") break;}/* close socket */close (sockfd); return 0 ;}
This server needs to implement the function: Create a UDP socket and wait for the datagram on Port 2000. after receiving the message Datagram, obtain the message sent from the client, send a welcome message to the client based on the address information of the sender contained in the Datagram, and finally determine whether the message is an exit command, if not, continue the above work; otherwise, the server process will be terminated.