服务端客户端相互通信的实现,请多线程实现,请给出相应c代码
时间: 2023-12-06 15:03:11 浏览: 60
多线程实现服务器和客户端聊天.pdf
以下是一个简单的多线程客户端和服务器端通信的示例代码,使用C语言编写。
服务器端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#define MAX_CLIENTS 5
#define BUF_SIZE 1024
void *connection_handler(void *);
int main(int argc, char *argv[]) {
int socket_desc, client_sock, c, *new_sock;
struct sockaddr_in server, client;
char *message;
// 创建套接字
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1) {
printf("Could not create socket");
}
// 准备服务器结构体
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(8888);
// 绑定套接字
if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("bind failed. Error");
return 1;
}
// 监听套接字
listen(socket_desc, MAX_CLIENTS);
printf("Waiting for incoming connections...\n");
c = sizeof(struct sockaddr_in);
while ((client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t *)&c))) {
printf("Connection accepted from %s:%d\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
// 创建新线程处理连接
pthread_t sniffer_thread;
new_sock = malloc(1);
*new_sock = client_sock;
if (pthread_create(&sniffer_thread, NULL, connection_handler, (void *)new_sock) < 0) {
perror("could not create thread");
return 1;
}
printf("Handler assigned\n");
}
if (client_sock < 0) {
perror("accept failed");
return 1;
}
return 0;
}
void *connection_handler(void *socket_desc) {
int sock = *(int *)socket_desc;
int read_size;
char client_message[BUF_SIZE];
// 接收消息并发送回复
while ((read_size = recv(sock, client_message, BUF_SIZE, 0)) > 0) {
printf("Received message: %s\n", client_message);
if (write(sock, client_message, strlen(client_message)) < 0) {
perror("write failed");
return NULL;
}
memset(client_message, 0, BUF_SIZE);
}
if (read_size == 0) {
printf("Client disconnected\n");
} else if (read_size == -1) {
perror("recv failed");
}
free(socket_desc);
return NULL;
}
```
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define BUF_SIZE 1024
int main(int argc, char *argv[]) {
int sock;
struct sockaddr_in server;
char message[BUF_SIZE], server_reply[BUF_SIZE];
// 创建套接字
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
printf("Could not create socket");
}
// 准备服务器结构体
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons(8888);
// 连接服务器
if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("connect failed. Error");
return 1;
}
printf("Connected to server\n");
// 发送消息并接收回复
while (1) {
printf("Enter message: ");
fgets(message, BUF_SIZE, stdin);
if (write(sock, message, strlen(message)) < 0) {
perror("write failed");
return 1;
}
if (read(sock, server_reply, BUF_SIZE) < 0) {
perror("read failed");
return 1;
}
printf("Server reply: %s\n", server_reply);
memset(message, 0, BUF_SIZE);
memset(server_reply, 0, BUF_SIZE);
}
close(sock);
return 0;
}
```
该示例中,服务器可以同时处理多个客户端连接,每个客户端连接都会创建一个新线程来处理。在客户端,用户可以输入消息并发送给服务器,服务器接收消息并回复相同的消息给客户端。这个示例中仅仅只是简单的演示了客户端和服务器的基本通信流程,可以根据实际需求进行调整和扩展。
阅读全文