UDP (User Datagram Protocol) is a connectionless transport layer protocol, widely used in scenarios with high real-time requirements, such as video streaming, voice communication, online games, etc. Unlike TCP, UDP does not guarantee data reliability and sequence, but it transmits faster. This article will explain how to implement a simple UDP client program in C language and how to communicate with the server.
1. Introduction to UDP protocol
The characteristics of the UDP protocol are as follows:
No connection: UDP does not need to establish a connection, and can directly send data to the target host.
Unreliability: UDP does not guarantee the successful arrival or receipt of data in sequence, and data loss or duplication may occur.
Message-oriented: UDP is transmitted in units of independent messages (datagrams), and each message is sent independently.
High transmission efficiency: UDP has high transmission efficiency because there is no need to establish a connection and maintenance state.
2. Implementing UDP client based on C language
Next, we will implement a simple UDP client through C language. The client will send a message to the server and receive a response from the server.
1. Necessary header files
To implement network programming in C language, the following header files are usually required:
#include <> #include <> #include <> #include <arpa/> #include <sys/> #include <>
These header files provide the basic functions and data structures required for network programming.
2. Implement the core code of UDP client
Below is a simple UDP client implementation. The client will send a message to the server and wait for the reception of the server's response.
#define SERVER_PORT 12345 // Server port number#define SERVER_IP "127.0.0.1" // Server IP address#define BUFFER_SIZE 1024 // Buffer size int main() { int sockfd; struct sockaddr_in server_addr; char buffer[BUFFER_SIZE]; socklen_t addr_len; ssize_t n; // Create UDP socket sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd < 0) { perror("Socket creation failed"); exit(EXIT_FAILURE); } // Configure the server address memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; // Use IPv4 address server_addr.sin_port = htons(SERVER_PORT); // Set the port number and convert it to network byte order inet_pton(AF_INET, SERVER_IP, &server_addr.sin_addr); // Convert IP address from text format to binary format // Send a message to the server const char *message = "Hello, UDP Server!"; sendto(sockfd, message, strlen(message), 0, (struct sockaddr *)&server_addr, sizeof(server_addr)); printf("Message sent to the server: %s\n", message); // Receive the server's response addr_len = sizeof(server_addr); n = recvfrom(sockfd, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&server_addr, &addr_len); if (n > 0) { buffer[n] = '\0'; // Make sure the string ends with '\0' printf("Received server response: %s\n", buffer); } else { printf("No response from the server was received or received error.\n"); } // Close the socket close(sockfd); return 0; }
3. Code parsing
Create UDP socket: socket(AF_INET, SOCK_DGRAM, 0) is used to create UDP socket. AF_INET means using IPv4 protocol, and SOCK_DGRAM means using UDP protocol.
Configure server address: The server_addr structure saves the server's IP address and port number. The inet_pton() function is used to convert the IP address from a text format (such as "127.0.0.1") to a network byte order.
Send message to the server: The sendto() function is used to send messages to the specified server address.
Receive the server's response: The recvfrom() function is used to receive messages from the server and obtain the server's address information.
Close socket: When the program ends, call the close() function to close the socket and release the resource.
4. Compile and run
The above code can be compiled and run using the following command:
gcc -o udp_client udp_client.c
./udp_client
After running, the client will send a message to 127.0.0.1:12345 (i.e., port 12345 of the local host) and wait for the server to respond.
5. Test the UDP client
To test the UDP client, a running UDP server is required. Here is a simple UDP server code example that can be used to test the client's functionality:
#include <> #include <> #include <> #include <arpa/> #include <sys/> #include <> #define PORT 12345 #define BUFFER_SIZE 1024 int main() { int sockfd; char buffer[BUFFER_SIZE]; struct sockaddr_in server_addr, client_addr; socklen_t addr_len; ssize_t n; // Create UDP socket sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd < 0) { perror("Socket creation failed"); exit(EXIT_FAILURE); } // Configure the server address memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = INADDR_ANY; server_addr.sin_port = htons(PORT); // Bind socket if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { perror("Binding failed"); close(sockfd); exit(EXIT_FAILURE); } printf("UDP server is started, listening to port %d...\n", PORT); // Receive client messages addr_len = sizeof(client_addr); n = recvfrom(sockfd, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&client_addr, &addr_len); if (n > 0) { buffer[n] = '\0'; printf("Message received from client: %s\n", buffer); // Send a response to the client const char *response = "The server has received your message"; sendto(sockfd, response, strlen(response), 0, (struct sockaddr *)&client_addr, addr_len); } // Close the socket close(sockfd); return 0; }
After running this server, the client can communicate with it.
3. Summary
This article describes how to implement a simple UDP client in C language and demonstrates the basic communication process with the server through sample code. Due to its high transmission efficiency, the UDP protocol is very suitable for application scenarios with high requirements for real-time performance. However, since UDP does not guarantee data reliability and sequence, additional mechanisms may be needed to enhance the reliability of data transmission in practical applications.
This is the end of this article about implementing UDP client based on C language. For more related content in C language UDP client, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!