用C语言编写一个udp通信程序,包含服务器和客户端代码,服务器端实现多线程编程。
时间: 2024-03-20 14:44:08 浏览: 77
好的,以下是一个简单的UDP通信程序,包含服务器和客户端代码,服务器端实现了多线程编程。
服务器端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <pthread.h>
#define MAXBUF 1024
#define PORT 8888
void *handle_client(void *arg)
{
int client_socket = *(int*)arg;
struct sockaddr_in client_addr;
socklen_t client_addr_size = sizeof(client_addr);
while(1) {
char buffer[MAXBUF+1];
memset(buffer, 0, sizeof(buffer));
ssize_t len = recvfrom(client_socket, buffer, sizeof(buffer)-1, 0, (struct sockaddr*)&client_addr, &client_addr_size);
if(len == -1) {
perror("recvfrom error");
break;
}
if(len == 0) {
printf("client closed\n");
break;
}
printf("Received from %s:%d: %s", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), buffer);
sendto(client_socket, buffer, strlen(buffer), 0, (struct sockaddr*)&client_addr, client_addr_size);
}
close(client_socket);
free(arg);
pthread_exit(NULL);
}
int main()
{
int server_socket;
struct sockaddr_in server_addr;
// 创建套接字
server_socket = socket(AF_INET, SOCK_DGRAM, 0);
if(server_socket == -1) {
perror("socket error");
exit(EXIT_FAILURE);
}
// 绑定地址和端口
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(PORT);
if(bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
perror("bind error");
exit(EXIT_FAILURE);
}
printf("Server started on %s:%d\n", inet_ntoa(server_addr.sin_addr), ntohs(server_addr.sin_port));
// 循环处理客户端请求
while(1) {
// 接收客户端连接
int *client_socket = (int*)malloc(sizeof(int));
*client_socket = accept(server_socket, NULL, NULL);
if(*client_socket == -1) {
perror("accept error");
continue;
}
// 创建线程处理客户端请求
pthread_t tid;
if(pthread_create(&tid, NULL, handle_client, client_socket) != 0) {
perror("pthread_create error");
continue;
}
pthread_detach(tid); // 线程结束时自动回收资源
}
// 关闭服务器套接字
close(server_socket);
return 0;
}
```
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define MAXBUF 1024
#define PORT 8888
int main()
{
int client_socket;
struct sockaddr_in server_addr;
// 创建套接字
client_socket = socket(AF_INET, SOCK_DGRAM, 0);
if(client_socket == -1) {
perror("socket error");
exit(EXIT_FAILURE);
}
// 设置服务器地址和端口号
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // 服务器IP地址
server_addr.sin_port = htons(PORT);
// 循环处理用户输入
while(1) {
// 读取用户输入
char buffer[MAXBUF+1];
printf("Enter message to send: ");
fgets(buffer, sizeof(buffer), stdin);
if(feof(stdin)) {
break;
}
// 发送数据
if(sendto(client_socket, buffer, strlen(buffer), 0, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
perror("sendto error");
continue;
}
// 接收数据
memset(buffer, 0, sizeof(buffer));
ssize_t len = recvfrom(client_socket, buffer, sizeof(buffer)-1, 0, NULL, NULL);
if(len == -1) {
perror("recvfrom error");
continue;
}
if(len == 0) {
printf("server closed\n");
break;
}
printf("Received from server: %s", buffer);
}
// 关闭客户端套接字
close(client_socket);
return 0;
}
```
在运行程序前,需要在终端中启动服务器端程序:
```
$ gcc -o server server.c -lpthread
$ ./server
```
然后在另一个终端中启动客户端程序:
```
$ gcc -o client client.c
$ ./client
```
接着,客户端会要求您输入要发送的消息,您可以输入任何内容并按回车键发送。服务器将收到该消息并发送一个确认消息。客户端将收到确认消息并输出它们。
阅读全文