C语言多线程服务器客户端相互收发,并写出具体代码
时间: 2024-03-26 11:40:27 浏览: 207
好的,下面是一个简单的示例代码,实现了一个简单的多线程服务器和客户端之间的相互收发数据:
服务器端代码:
```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 PORT 8000
#define MAX_CLIENTS 10
#define MAX_MSG_LENGTH 1024
int client_sockets[MAX_CLIENTS]; // 保存客户端套接字的数组
// 线程处理函数,用于与客户端进行通信
void *thread_func(void *arg) {
int client_sock = *(int *)arg;
char recv_buffer[MAX_MSG_LENGTH];
char send_buffer[MAX_MSG_LENGTH];
while (1) {
// 接收客户端发送的数据
memset(recv_buffer, 0, sizeof(recv_buffer));
if (recv(client_sock, recv_buffer, sizeof(recv_buffer), 0) < 0) {
perror("recv failed");
close(client_sock);
pthread_exit(NULL);
}
printf("Received message from client %d: %s\n", client_sock, recv_buffer);
// 发送数据给客户端
memset(send_buffer, 0, sizeof(send_buffer));
snprintf(send_buffer, sizeof(send_buffer), "Hello, client %d", client_sock);
if (send(client_sock, send_buffer, strlen(send_buffer), 0) < 0) {
perror("send failed");
close(client_sock);
pthread_exit(NULL);
}
}
}
int main() {
int server_sock, client_sock;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_len;
pthread_t thread_id;
int i;
// 创建服务器端套接字
if ((server_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// 设置服务器端地址结构体
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;
// 绑定服务器端套接字到指定地址和端口
if (bind(server_sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// 监听客户端连接请求
if (listen(server_sock, MAX_CLIENTS) < 0) {
perror("listen failed");
exit(EXIT_FAILURE);
}
printf("Server started, waiting for clients...\n");
// 接收客户端连接请求,并创建一个新线程来处理客户端请求
while (1) {
client_addr_len = sizeof(client_addr);
if ((client_sock = accept(server_sock, (struct sockaddr *)&client_addr, &client_addr_len)) < 0) {
perror("accept failed");
continue;
}
// 将新连接的客户端套接字保存到数组中
for (i = 0; i < MAX_CLIENTS; i++) {
if (client_sockets[i] == 0) {
client_sockets[i] = client_sock;
break;
}
}
if (i == MAX_CLIENTS) {
printf("Too many clients, connection rejected.\n");
close(client_sock);
continue;
}
printf("New client connected: %s:%d, assigned ID %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), i);
// 创建一个新线程来处理该客户端请求
if (pthread_create(&thread_id, NULL, thread_func, &client_sock) < 0) {
perror("pthread_create failed");
continue;
}
pthread_detach(thread_id); // 分离线程
}
return 0;
}
```
客户端代码:
```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 SERVER_ADDR "127.0.0.1"
#define PORT 8000
#define MAX_MSG_LENGTH 1024
// 线程处理函数,用于与服务器进行通信
void *thread_func(void *arg) {
int server_sock = *(int *)arg;
char recv_buffer[MAX_MSG_LENGTH];
char send_buffer[MAX_MSG_LENGTH];
while (1) {
// 发送数据给服务器
memset(send_buffer, 0, sizeof(send_buffer));
snprintf(send_buffer, sizeof(send_buffer), "Hello, server");
if (send(server_sock, send_buffer, strlen(send_buffer), 0) < 0) {
perror("send failed");
close(server_sock);
pthread_exit(NULL);
}
// 接收服务器发送的数据
memset(recv_buffer, 0, sizeof(recv_buffer));
if (recv(server_sock, recv_buffer, sizeof(recv_buffer), 0) < 0) {
perror("recv failed");
close(server_sock);
pthread_exit(NULL);
}
printf("Received message from server: %s\n", recv_buffer);
sleep(1); // 暂停1秒
}
}
int main() {
int server_sock;
struct sockaddr_in server_addr;
pthread_t thread_id;
// 创建客户端套接字
if ((server_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// 设置服务器端地址结构体
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
if (inet_pton(AF_INET, SERVER_ADDR, &server_addr.sin_addr) <= 0) {
perror("inet_pton failed");
exit(EXIT_FAILURE);
}
// 连接服务器端套接字
if (connect(server_sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("connect failed");
exit(EXIT_FAILURE);
}
printf("Connected to server %s:%d\n", SERVER_ADDR, PORT);
// 创建一个新线程来处理与服务器的通信
if (pthread_create(&thread_id, NULL, thread_func, &server_sock) < 0) {
perror("pthread_create failed");
exit(EXIT_FAILURE);
}
while (1) {
sleep(1); // 暂停1秒
}
return 0;
}
```
上面的代码仅作为示例,实际使用时需要根据具体需求进行修改和优化。
阅读全文