使用c语言编写一个udp通信程序,包含服务器和客户端代码,服务器端实现多线程编程。
时间: 2024-03-20 16:44:18 浏览: 81
以下是基于C语言的UDP通信程序,包含服务器和客户端代码,服务器端实现了多线程编程:
服务器端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#define BUF_SIZE 1024
#define PORT 8080
#define MAX_CLIENTS 100
void *handle_client(void *arg);
int main(int argc, char const *argv[]) {
int server_fd, client_fd;
struct sockaddr_in server_addr, client_addr;
int opt = 1;
int addr_len = sizeof(struct sockaddr_in);
char buf[BUF_SIZE];
int ret;
// 创建socket
if ((server_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// 设置socket选项
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
perror("setsockopt failed");
exit(EXIT_FAILURE);
}
// 绑定地址
memset(&server_addr, 0, addr_len);
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr *)&server_addr, addr_len) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// 接收数据
pthread_t thread_id[MAX_CLIENTS];
int i = 0;
while (1) {
memset(buf, 0, BUF_SIZE);
ret = recvfrom(server_fd, buf, BUF_SIZE, 0, (struct sockaddr *)&client_addr, &addr_len);
if (ret < 0) {
perror("recvfrom failed");
exit(EXIT_FAILURE);
}
// 处理客户端请求
printf("Received message from %s:%d: %s\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), buf);
pthread_create(&thread_id[i++], NULL, handle_client, (void *)&client_fd);
}
return 0;
}
// 处理客户端请求
void *handle_client(void *arg) {
int client_fd = *(int *)arg;
char buf[BUF_SIZE];
int ret;
while (1) {
memset(buf, 0, BUF_SIZE);
ret = recv(client_fd, buf, BUF_SIZE, 0);
if (ret < 0) {
perror("recv failed");
exit(EXIT_FAILURE);
} else if (ret == 0) {
printf("Client disconnected\n");
break;
}
printf("Received message: %s\n", buf);
ret = send(client_fd, buf, strlen(buf), 0);
if (ret < 0) {
perror("send failed");
exit(EXIT_FAILURE);
}
}
close(client_fd);
return NULL;
}
```
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define BUF_SIZE 1024
#define PORT 8080
int main(int argc, char const *argv[]) {
int client_fd;
struct sockaddr_in server_addr;
char buf[BUF_SIZE];
int ret;
// 创建socket
if ((client_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket 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);
// 发送数据
while (1) {
memset(buf, 0, BUF_SIZE);
fgets(buf, BUF_SIZE, stdin);
ret = sendto(client_fd, buf, strlen(buf), 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (ret < 0) {
perror("sendto failed");
exit(EXIT_FAILURE);
}
}
close(client_fd);
return 0;
}
```
在服务器端,我们使用`pthread_create()`函数创建线程来处理客户端请求。在客户端,我们使用`fgets()`函数从标准输入读取用户输入,并使用`sendto()`函数将数据发送到服务器。
阅读全文