使用C语言编写一个简单的UDP通信程序,包含服务器和客户端代码,服务器端实现多线程编程。
时间: 2024-03-20 19:44:16 浏览: 75
好的,以下是一个简单的UDP通信程序,包含服务器和客户端代码,服务器端使用多线程编程实现:
服务器端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8888
#define MAXLINE 1024
void *process(void *arg);
int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr, cliaddr;
pthread_t tid;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
perror("socket error");
exit(1);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
perror("bind error");
exit(1);
}
printf("Server started.\n");
while (1) {
socklen_t len = sizeof(cliaddr);
char buf[MAXLINE];
int n = recvfrom(sockfd, buf, MAXLINE, 0, (struct sockaddr *)&cliaddr, &len);
if (n < 0) {
perror("recvfrom error");
continue;
}
buf[n] = '\0';
printf("Received message from %s:%d: %s\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port), buf);
// 创建线程处理客户端请求
pthread_create(&tid, NULL, process, (void *)&sockfd);
}
return 0;
}
void *process(void *arg)
{
int sockfd = *(int *)arg;
struct sockaddr_in cliaddr;
socklen_t len = sizeof(cliaddr);
char buf[MAXLINE];
int n = recvfrom(sockfd, buf, MAXLINE, 0, (struct sockaddr *)&cliaddr, &len);
if (n < 0) {
perror("recvfrom error");
pthread_exit(NULL);
}
buf[n] = '\0';
printf("Received message from %s:%d: %s\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port), buf);
// 处理客户端请求
char *sendbuf = "Response from server.";
n = sendto(sockfd, sendbuf, strlen(sendbuf), 0, (struct sockaddr *)&cliaddr, len);
if (n < 0) {
perror("sendto error");
}
pthread_exit(NULL);
}
```
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8888
#define MAXLINE 1024
int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr;
if (argc != 2) {
printf("Usage: %s <IP address>\n", argv[0]);
exit(1);
}
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
perror("socket error");
exit(1);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(argv[1]);
servaddr.sin_port = htons(PORT);
char sendbuf[MAXLINE] = "Hello, server.";
int n = sendto(sockfd, sendbuf, strlen(sendbuf), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
if (n < 0) {
perror("sendto error");
exit(1);
}
char recvbuf[MAXLINE];
n = recvfrom(sockfd, recvbuf, MAXLINE, 0, NULL, NULL);
if (n < 0) {
perror("recvfrom error");
exit(1);
}
recvbuf[n] = '\0';
printf("Received response from server: %s\n", recvbuf);
close(sockfd);
return 0;
}
```
这个程序使用UDP协议进行通信,服务器端监听本地的8888端口,接收客户端发送的消息,并创建线程处理客户端请求。客户端向服务器发送消息,并等待服务器的响应。
阅读全文