[toc]
用C语言在Linux中使用UDP协议发送文本消息 1.用C语言在Linux中使用UDP协议发送一句文本消息 1.使用C语言在Linux中创建服务端程序接收一句文本消息 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <errno.h> #define BUF_SIZE 256 int main () { int sock; struct sockaddr_in serverAddr , clientAddr ; char buffer[BUF_SIZE]; socklen_t clientAddrLen = sizeof (clientAddr); sock = socket(AF_INET, SOCK_DGRAM, 0 ); if (sock < 0 ) { perror("Socket creation failed" ); return 1 ; } serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons(8888 ); serverAddr.sin_addr.s_addr = INADDR_ANY; if (bind(sock, (struct sockaddr*)&serverAddr, sizeof (serverAddr)) < 0 ) { perror("Bind failed" ); close(sock); return 1 ; } printf ("UDP server is listening on port 8888...\n" ); int recvLen = recvfrom(sock, buffer, BUF_SIZE, 0 , (struct sockaddr*)&clientAddr, &clientAddrLen); if (recvLen < 0 ) { perror("Receive failed" ); close(sock); return 1 ; } buffer[recvLen] = '\0' ; char clientIP[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &clientAddr.sin_addr, clientIP, sizeof (clientIP)); printf ("Received message from %s:%d\n" , clientIP, ntohs(clientAddr.sin_port)); printf ("%s\n" , buffer); close(sock); return 0 ; }
2.使用C语言在Linux中创建客户端程序发送一句文本消息 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <errno.h> #define BUF_SIZE 256 int main () { int sock; struct sockaddr_in serverAddr ; char message[BUF_SIZE]; sock = socket(AF_INET, SOCK_DGRAM, 0 ); if (sock < 0 ) { perror("Socket creation failed" ); return 1 ; } serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons(8888 ); serverAddr.sin_addr.s_addr = INADDR_ANY; if (inet_pton(AF_INET, "127.0.0.1" , &serverAddr.sin_addr) <= 0 ) { perror("Invalid address/ Address not supported" ); close(sock); return 1 ; } printf ("Enter message to send: " ); fgets(message, BUF_SIZE, stdin ); ssize_t sendResult = sendto(sock, message, strlen (message), 0 , (struct sockaddr*)&serverAddr, sizeof (serverAddr)); if (sendResult < 0 ) { perror("Send failed" ); close(sock); return 1 ; } printf ("Message sent: %s\n" , message); close(sock); return 0 ; }
3.运行结果
运行LinServer
运行LinClient
运行成功
2.用C语言在Linux中使用UDP协议多次发送文本消息 1.使用C语言在Linux中创建服务端程序多次接收文本消息 在 UDP 协议中,”断开连接” 的概念与 TCP 不同,因为 UDP 是无连接的。UDP 服务器无法主动检测客户端的断开状态,因为它不维护连接状态。UDP 服务器只能在接收到数据包时进行响应。
不过,可以通过约定某种特定的消息(例如,客户端发送一个特定的 “断开连接” 消息)来模拟这种行为。以下是修改后的代码示例,当接收到特定消息(例如 “exit”)时,服务器将退出循环并关闭连接:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <errno.h> int main () { int serverSocket; struct sockaddr_in server , client ; socklen_t clientSize; char buffer[1024 ]; ssize_t recvSize; serverSocket = socket(AF_INET, SOCK_DGRAM, 0 ); if (serverSocket < 0 ) { perror("Could not create socket" ); return 1 ; } server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons(8888 ); if (bind(serverSocket, (struct sockaddr*)&server, sizeof (server)) < 0 ) { perror("Bind failed" ); close(serverSocket); return 1 ; } printf ("Waiting for incoming messages...\n" ); clientSize = sizeof (client); while (1 ) { recvSize = recvfrom(serverSocket, buffer, sizeof (buffer) - 1 , 0 , (struct sockaddr*)&client, &clientSize); if (recvSize < 0 ) { perror("Receive failed" ); break ; } buffer[recvSize] = '\0' ; char clientIP[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &client.sin_addr, clientIP, sizeof (clientIP)); printf ("Received message from %s:%d: %s\n" , clientIP, ntohs(client.sin_port), buffer); if (strcmp (buffer, "exit" ) == 0 ) { printf ("Client requested to exit. Closing server.\n" ); break ; } } close(serverSocket); return 0 ; }
2.使用C语言在Linux中创建客户端程序多次发送文本消息 此程序当输入exit时,先发送消息给服务端再退出程序。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <errno.h> void sendMessage (int sock, struct sockaddr_in *server, const char *message) { ssize_t sendResult = sendto(sock, message, strlen (message), 0 , (struct sockaddr*)server, sizeof (*server)); if (sendResult < 0 ) { perror("Send failed" ); } else { printf ("Message sent: %s\n" , message); } } int main () { int sock; struct sockaddr_in server ; char message[1024 ]; sock = socket(AF_INET, SOCK_DGRAM, 0 ); if (sock < 0 ) { perror("Could not create socket" ); return 1 ; } server.sin_family = AF_INET; server.sin_port = htons(8888 ); if (inet_pton(AF_INET, "127.0.0.1" , &server.sin_addr) <= 0 ) { perror("Invalid address/ Address not supported" ); close(sock); return 1 ; } while (1 ) { printf ("Enter message (type 'exit' to quit): " ); fgets(message, sizeof (message), stdin ); message[strcspn (message, "\n" )] = 0 ; if (strcmp (message, "exit" ) == 0 ) { sendMessage(sock, &server, "exit" ); break ; } sendMessage(sock, &server, message); } close(sock); return 0 ; }
3.运行结果
运行LinServer
运行LinClient
运行成功
3.用C语言在Linux中使用UDP协议客户端退出后可以再次连接服务器 1.使用C语言在Linux中创建服务端程序断开后等待客户端连接 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <errno.h> int main () { int serverSocket; struct sockaddr_in server , client ; socklen_t clientSize; char buffer[1024 ]; ssize_t recvSize; serverSocket = socket(AF_INET, SOCK_DGRAM, 0 ); if (serverSocket < 0 ) { perror("Could not create socket" ); return 1 ; } server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons(8888 ); if (bind(serverSocket, (struct sockaddr*)&server, sizeof (server)) < 0 ) { perror("Bind failed" ); close(serverSocket); return 1 ; } printf ("Waiting for incoming messages...\n" ); clientSize = sizeof (client); while (1 ) { recvSize = recvfrom(serverSocket, buffer, sizeof (buffer) - 1 , 0 , (struct sockaddr*)&client, &clientSize); if (recvSize < 0 ) { perror("Receive failed" ); continue ; } buffer[recvSize] = '\0' ; char clientIP[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &client.sin_addr, clientIP, sizeof (clientIP)); printf ("Received message from %s:%d: %s\n" , clientIP, ntohs(client.sin_port), buffer); } close(serverSocket); return 0 ; }
2.使用C语言在Linux中创建客户端程序退出后再次连接服务端 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> int main () { int sock; struct sockaddr_in server ; char message[1024 ]; ssize_t sendResult; sock = socket(AF_INET, SOCK_DGRAM, 0 ); if (sock < 0 ) { perror("Could not create socket" ); return 1 ; } server.sin_family = AF_INET; server.sin_port = htons(8888 ); if (inet_pton(AF_INET, "127.0.0.1" , &server.sin_addr) <= 0 ) { perror("Invalid address/ Address not supported" ); close(sock); return 1 ; } while (1 ) { printf ("Enter message (type 'exit' to quit): " ); fgets(message, sizeof (message), stdin ); message[strcspn (message, "\n" )] = 0 ; if (strcmp (message, "exit" ) == 0 ) { break ; } sendResult = sendto(sock, message, strlen (message), 0 , (struct sockaddr*)&server, sizeof (server)); if (sendResult < 0 ) { perror("Send failed" ); break ; } else { printf ("Message sent: %s\n" , message); } } close(sock); return 0 ; }
3.运行结果
运行LinServer
运行LinClient
运行成功
4.用C语言在Linux中使用UDP协议让客户端与服务端相互发送文本消息 1.使用C语言在Linux中创建服务端接收文本消息后再发送到客户端 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> int main () { int serverSocket; struct sockaddr_in server , client ; socklen_t clientSize; char buffer[1024 ]; ssize_t recvSize; serverSocket = socket(AF_INET, SOCK_DGRAM, 0 ); if (serverSocket < 0 ) { perror("Could not create socket" ); return 1 ; } server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons(8888 ); if (bind(serverSocket, (struct sockaddr*)&server, sizeof (server)) < 0 ) { perror("Bind failed" ); close(serverSocket); return 1 ; } printf ("Waiting for incoming messages...\n" ); clientSize = sizeof (client); while (1 ) { recvSize = recvfrom(serverSocket, buffer, sizeof (buffer) - 1 , 0 , (struct sockaddr*)&client, &clientSize); if (recvSize < 0 ) { perror("Receive failed" ); continue ; } buffer[recvSize] = '\0' ; char clientIP[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &client.sin_addr, clientIP, sizeof (clientIP)); printf ("Received message from %s:%d: %s\n" , clientIP, ntohs(client.sin_port), buffer); sendto(serverSocket, buffer, recvSize, 0 , (struct sockaddr*)&client, clientSize); } close(serverSocket); return 0 ; }
2.使用C语言在Linux中创建客户端程序接收与发送文本消息 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> int main () { int sock; struct sockaddr_in server ; char message[1024 ]; char buffer[1024 ]; ssize_t sendResult, recvSize; sock = socket(AF_INET, SOCK_DGRAM, 0 ); if (sock < 0 ) { perror("Could not create socket" ); return 1 ; } server.sin_family = AF_INET; server.sin_port = htons(8888 ); if (inet_pton(AF_INET, "127.0.0.1" , &server.sin_addr) <= 0 ) { perror("Invalid address/ Address not supported" ); close(sock); return 1 ; } while (1 ) { printf ("Enter message (type 'exit' to quit): " ); fgets(message, sizeof (message), stdin ); message[strcspn (message, "\n" )] = 0 ; if (strcmp (message, "exit" ) == 0 ) { break ; } sendResult = sendto(sock, message, strlen (message), 0 , (struct sockaddr*)&server, sizeof (server)); if (sendResult < 0 ) { perror("Send failed" ); break ; } else { printf ("Message sent: %s\n" , message); } recvSize = recvfrom(sock, buffer, sizeof (buffer) - 1 , 0 , NULL , NULL ); if (recvSize > 0 ) { buffer[recvSize] = '\0' ; printf ("Received from server: %s\n" , buffer); } else if (recvSize < 0 ) { perror("Receive failed" ); break ; } } close(sock); return 0 ; }
3.运行结果
运行LinServer
运行LinClient
运行成功
5.用C语言在Linux中使用UDP协议写一个聊天室让两个客户端之间可以相互发送文本消息 1.使用C语言在Linux中创建服务端接收与转发文本消息到所有客户端中 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #define PORT 8080 #define BUFFER_SIZE 1024 #define MAX_CLIENTS 10 int main () { int sockfd; char buffer[BUFFER_SIZE]; struct sockaddr_in server_addr , client_addr ; socklen_t addr_len = sizeof (client_addr); struct sockaddr_in clients [MAX_CLIENTS ]; int client_count = 0 ; int first_message[MAX_CLIENTS] = {0 }; sockfd = socket(AF_INET, SOCK_DGRAM, 0 ); if (sockfd < 0 ) { perror("socket creation failed" ); exit (EXIT_FAILURE); } 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); if (bind(sockfd, (const struct sockaddr *)&server_addr, sizeof (server_addr)) < 0 ) { perror("bind failed" ); close(sockfd); exit (EXIT_FAILURE); } while (1 ) { int n = recvfrom(sockfd, buffer, BUFFER_SIZE, 0 , (struct sockaddr *)&client_addr, &addr_len); buffer[n] = '\0' ; int found = 0 ; int client_index = -1 ; for (int i = 0 ; i < client_count; i++) { if (clients[i].sin_addr.s_addr == client_addr.sin_addr.s_addr && clients[i].sin_port == client_addr.sin_port) { found = 1 ; client_index = i; break ; } } if (!found && client_count < MAX_CLIENTS) { clients[client_count++] = client_addr; client_index = client_count - 1 ; printf ("New client added: %s:%d\n" , inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); } if (client_index != -1 ) { if (first_message[client_index] == 0 ) { first_message[client_index] = 1 ; } else { for (int i = 0 ; i < client_count; i++) { if (clients[i].sin_addr.s_addr != client_addr.sin_addr.s_addr || clients[i].sin_port != client_addr.sin_port) { sendto(sockfd, buffer, n, 0 , (const struct sockaddr *)&clients[i], sizeof (clients[i])); } } } } } close(sockfd); return 0 ; }
2.使用C语言在Linux中创建客户端程序接收与发送文本消息 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <pthread.h> #define PORT 8080 #define SERVER_IP "127.0.0.1" #define BUFFER_SIZE 1024 void *receive_messages (void *sockfd) { int socket_fd = *(int *)sockfd; char buffer[BUFFER_SIZE]; struct sockaddr_in server_addr ; socklen_t addr_len = sizeof (server_addr); while (1 ) { int n = recvfrom(socket_fd, buffer, BUFFER_SIZE, 0 , (struct sockaddr *)&server_addr, &addr_len); if (n > 0 ) { buffer[n] = '\0' ; printf ("Received: %s\n" , buffer); } } return NULL ; } int main () { int sockfd; struct sockaddr_in server_addr ; sockfd = socket(AF_INET, SOCK_DGRAM, 0 ); if (sockfd < 0 ) { perror("socket creation failed" ); exit (EXIT_FAILURE); } memset (&server_addr, 0 , sizeof (server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(PORT); inet_pton(AF_INET, SERVER_IP, &server_addr.sin_addr); const char *initial_message = "Hello, server!" ; sendto(sockfd, initial_message, strlen (initial_message), 0 , (const struct sockaddr *)&server_addr, sizeof (server_addr)); pthread_t recv_thread; if (pthread_create(&recv_thread, NULL , receive_messages, (void *)&sockfd) != 0 ) { perror("Failed to create thread" ); close(sockfd); exit (EXIT_FAILURE); } char message[BUFFER_SIZE]; while (1 ) { printf ("Enter message: " ); fgets(message, BUFFER_SIZE, stdin ); message[strcspn (message, "\n" )] = 0 ; sendto(sockfd, message, strlen (message), 0 , (const struct sockaddr *)&server_addr, sizeof (server_addr)); } pthread_join(recv_thread, NULL ); close(sockfd); return 0 ; }
3.运行结果
运行LinServer
运行LinClient
运行第二个LinClient
在LinuxClient中输入消息,在另一个LinuxClient中查看接收到的消息。
运行后的结果符合预期
6.用C语言在Linux中使用UDP协议与ipv6协议发送一句文本消息 1.使用C语言在Linux中使用ipv6地址创建服务端程序 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <netdb.h> #define PORT "8080" #define BUF_SIZE 1024 int main () { int sockfd; struct addrinfo hints , *res ; struct sockaddr_storage client_addr ; socklen_t addr_len = sizeof (client_addr); char buffer[BUF_SIZE]; ssize_t recvResult; memset (&hints, 0 , sizeof (hints)); hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_DGRAM; hints.ai_flags = AI_PASSIVE; if (getaddrinfo(NULL , PORT, &hints, &res) != 0 ) { perror("getaddrinfo failed" ); return 1 ; } sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if (sockfd < 0 ) { perror("socket failed" ); freeaddrinfo(res); return 1 ; } if (bind(sockfd, res->ai_addr, res->ai_addrlen) < 0 ) { perror("bind failed" ); close(sockfd); freeaddrinfo(res); return 1 ; } freeaddrinfo(res); printf ("UDP Server is listening on port %s...\n" , PORT); while (1 ) { recvResult = recvfrom(sockfd, buffer, sizeof (buffer) - 1 , 0 , (struct sockaddr*)&client_addr, &addr_len); if (recvResult < 0 ) { perror("recvfrom failed" ); continue ; } buffer[recvResult] = '\0' ; printf ("Received message: %s\n" , buffer); sendto(sockfd, buffer, recvResult, 0 , (struct sockaddr*)&client_addr, addr_len); } close(sockfd); return 0 ; }
2.使用C语言在Linux中使用ipv6地址创建客户端程序 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <netdb.h> #define PORT "8080" #define SERVER_ADDR "::1" #define BUF_SIZE 256 int main () { int sockfd; struct addrinfo hints , *res ; const char * message = "Hello, UDP Server!" ; ssize_t sendResult; memset (&hints, 0 , sizeof (hints)); hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_DGRAM; hints.ai_flags = 0 ; if (getaddrinfo(SERVER_ADDR, PORT, &hints, &res) != 0 ) { perror("getaddrinfo failed" ); return 1 ; } sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if (sockfd < 0 ) { perror("socket failed" ); freeaddrinfo(res); return 1 ; } sendResult = sendto(sockfd, message, strlen (message), 0 , res->ai_addr, res->ai_addrlen); if (sendResult < 0 ) { perror("sendto failed" ); close(sockfd); freeaddrinfo(res); return 1 ; } printf ("Message sent: %s\n" , message); char buffer[BUF_SIZE]; struct sockaddr_storage from_addr ; socklen_t addr_len = sizeof (from_addr); ssize_t recvResult = recvfrom(sockfd, buffer, sizeof (buffer) - 1 , 0 , (struct sockaddr*)&from_addr, &addr_len); if (recvResult > 0 ) { buffer[recvResult] = '\0' ; printf ("Received from server: %s\n" , buffer); } else { perror("recvfrom failed" ); } close(sockfd); freeaddrinfo(res); return 0 ; }
3.运行结果
运行LinServer
运行LinClient
运行成功