基于c语言编写一个udp通信程序,包含服务器和客户端代码,服务器端实现多线程编程。
时间: 2024-03-20 22:45:11 浏览: 85
好的,以下是基于C语言编写的UDP通信程序,包含服务器和客户端代码,服务器端实现多线程编程的代码示例:
服务器端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define MAX_MSG_SIZE 1024
#define SERVER_PORT 8888
#define MAX_CLIENTS 5
void *handle_client(void *arg) {
int sockfd = *(int *)arg;
char buf[MAX_MSG_SIZE];
struct sockaddr_in client_addr;
socklen_t addr_len = sizeof(client_addr);
while (1) {
memset(buf, 0, MAX_MSG_SIZE);
int len = recvfrom(sockfd, buf, MAX_MSG_SIZE, 0, (struct sockaddr *)&client_addr, &addr_len);
if (len == -1) {
perror("recvfrom error");
continue;
}
printf("Received message from client %s:%d: %s\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), buf);
len = sendto(sockfd, buf, len, 0, (struct sockaddr *)&client_addr, addr_len);
if (len == -1) {
perror("sendto error");
}
}
close(sockfd);
return NULL;
}
int main() {
int sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sockfd == -1) {
perror("socket error");
exit(EXIT_FAILURE);
}
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(SERVER_PORT);
if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("bind error");
exit(EXIT_FAILURE);
}
printf("Server started.\n");
pthread_t threads[MAX_CLIENTS];
int thread_count = 0;
while (thread_count < MAX_CLIENTS) {
int *client_sockfd = malloc(sizeof(int));
*client_sockfd = sockfd;
pthread_create(&threads[thread_count], NULL, handle_client, (void *)client_sockfd);
thread_count++;
}
for (int i = 0; i < MAX_CLIENTS; i++) {
pthread_join(threads[i], NULL);
}
close(sockfd);
printf("Server stopped.\n");
return 0;
}
```
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define SERVER_IP "127.0.0.1"
#define SERVER_PORT 8888
#define MAX_MSG_SIZE 1024
int main() {
int sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sockfd == -1) {
perror("socket error");
exit(EXIT_FAILURE);
}
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
server_addr.sin_port = htons(SERVER_PORT);
while (1) {
char buf[MAX_MSG_SIZE];
printf("Enter message to send: ");
fgets(buf, MAX_MSG_SIZE, stdin);
int len = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (len == -1) {
perror("sendto error");
continue;
}
memset(buf, 0, MAX_MSG_SIZE);
len = recvfrom(sockfd, buf, MAX_MSG_SIZE, 0, NULL, NULL);
if (len == -1) {
perror("recvfrom error");
continue;
}
printf("Received message from server: %s\n", buf);
}
close(sockfd);
return 0;
}
```
在服务器端,我们创建了一个UDP套接字,并将其绑定到本机的IP地址和端口号上。然后,我们使用多线程来处理客户端发送的消息,每个线程处理一个客户端的消息。
在客户端,我们同样创建了一个UDP套接字,并将其连接到服务器端的IP地址和端口号上。然后,我们使用一个无限循环来读取用户输入的消息,并将其发送给服务器端。同时,我们也接收服务器端返回的消息,并将其打印到屏幕上。
希望这个示例能够帮助你了解如何基于C语言编写UDP通信程序,并在服务器端使用多线程来处理客户端的消息。
阅读全文