使用C语言编写一个udp通信程序,包含服务器和客户端代码,服务器端实现多线程编程。并给出程序运行结果
时间: 2024-03-20 07:45:16 浏览: 72
好的,以下是一个简单的UDP通信程序,包括服务器端和客户端的代码。服务器端使用多线程编程,可以同时处理多个客户端请求。请注意,这只是一个简单的示例程序,实际应用中可能需要更复杂的处理方式。
服务器端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#define BUF_SIZE 1024
#define PORT 8080
void *handle_client(void *arg) {
int client_sock = *(int*)arg;
char buf[BUF_SIZE];
struct sockaddr_in client_addr;
socklen_t addr_len = sizeof(client_addr);
memset(&client_addr, 0, addr_len);
getpeername(client_sock, (struct sockaddr*)&client_addr, &addr_len);
printf("New connection from %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
while (1) {
int len = recv(client_sock, buf, BUF_SIZE, 0);
if (len <= 0) {
printf("%s:%d disconnected\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
break;
}
buf[len] = '\0';
printf("Received from %s:%d: %s", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), buf);
send(client_sock, buf, len, 0);
}
close(client_sock);
return NULL;
}
int main() {
int server_sock, client_sock;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_len = sizeof(client_addr);
memset(&server_addr, 0, sizeof(server_addr));
memset(&client_addr, 0, sizeof(client_addr));
server_sock = socket(AF_INET, SOCK_DGRAM, 0);
if (server_sock < 0) {
perror("socket");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
if (bind(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("bind");
exit(1);
}
printf("Server started at port %d\n", PORT);
while (1) {
char buf[BUF_SIZE];
int len = recvfrom(server_sock, buf, BUF_SIZE, 0, (struct sockaddr*)&client_addr, &addr_len);
if (len < 0) {
perror("recvfrom");
continue;
}
buf[len] = '\0';
printf("Received from %s:%d: %s", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), buf);
pthread_t tid;
if (pthread_create(&tid, NULL, handle_client, &client_sock) != 0) {
perror("pthread_create");
continue;
}
}
close(server_sock);
return 0;
}
```
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define BUF_SIZE 1024
#define SERVER_IP "127.0.0.1"
#define PORT 8080
int main() {
int sock;
struct sockaddr_in server_addr;
socklen_t addr_len = sizeof(server_addr);
memset(&server_addr, 0, sizeof(server_addr));
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
perror("socket");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
server_addr.sin_port = htons(PORT);
while (1) {
char buf[BUF_SIZE];
printf("Input message to send: ");
fgets(buf, BUF_SIZE, stdin);
sendto(sock, buf, strlen(buf), 0, (struct sockaddr*)&server_addr, addr_len);
int len = recvfrom(sock, buf, BUF_SIZE, 0, NULL, NULL);
if (len < 0) {
perror("recvfrom");
continue;
}
buf[len] = '\0';
printf("Received from server: %s", buf);
}
close(sock);
return 0;
}
```
运行结果:
在终端1中启动服务器端:
```
$ gcc server.c -o server -lpthread
$ ./server
Server started at port 8080
```
在终端2中启动客户端:
```
$ gcc client.c -o client
$ ./client
Input message to send: Hello
Received from server: Hello
Input message to send: World
Received from server: World
```
在终端2中输入“Hello”后,服务器端会输出:
```
Received from 127.0.0.1:48608: Hello
New connection from 127.0.0.1:48608
```
在终端2中输入“World”后,服务器端会输出:
```
Received from 127.0.0.1:48608: World
```
客户端会输出:
```
Received from server: Hello
Received from server: World
```
注意,这是一个基于UDP的简单示例程序,实际应用中可能需要更复杂的处理方式,例如处理粘包、拆包等问题。
阅读全文