c语言socket 的服务端和多个客户端通信
时间: 2023-10-02 20:04:39 浏览: 256
要实现C语言Socket的服务端和多个客户端通信,可以使用多线程技术,为每个客户端连接创建一个新的线程来处理通信。
以下是一个简单的示例代码:
1. 服务端
```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 MAX_CLIENTS 10 // 最大客户端数量
#define PORT 8888 // 监听端口号
void *client_handler(void *arg);
int main() {
int server_fd, client_fd, opt = 1;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_len = sizeof(struct sockaddr_in);
pthread_t tid[MAX_CLIENTS];
int i, ret;
// 创建服务器Socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// 设置Socket选项,允许地址重用
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt failed");
exit(EXIT_FAILURE);
}
// 绑定地址和端口号
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// 监听端口
if (listen(server_fd, MAX_CLIENTS) < 0) {
perror("listen failed");
exit(EXIT_FAILURE);
}
printf("Server: listening on port %d\n", PORT);
// 接受客户端连接并创建线程处理通信
while (1) {
if ((client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &addr_len)) < 0) {
perror("accept failed");
continue;
}
printf("Server: new client connected, IP address: %s, port number: %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
// 创建新线程处理客户端通信
for (i = 0; i < MAX_CLIENTS; i++) {
if (tid[i] == 0) {
if (pthread_create(&tid[i], NULL, client_handler, (void *)&client_fd) < 0) {
perror("pthread_create failed");
break;
}
break;
}
}
// 客户端数量已达到最大值,拒绝新连接
if (i == MAX_CLIENTS) {
printf("Server: max client connections reached, new connection rejected\n");
close(client_fd);
}
}
return 0;
}
// 客户端处理函数
void *client_handler(void *arg) {
int client_fd = *(int *)arg;
char buffer[1024] = {0};
int ret;
while (1) {
memset(buffer, 0, sizeof(buffer));
// 接收客户端消息
if ((ret = recv(client_fd, buffer, sizeof(buffer), 0)) <= 0) {
if (ret == 0) {
printf("Server: client disconnected\n");
} else {
perror("recv failed");
}
break;
}
// 处理客户端消息
printf("Server: received message from client: %s\n", buffer);
// 发送响应消息
if (send(client_fd, buffer, strlen(buffer), 0) < 0) {
perror("send failed");
break;
}
}
// 关闭连接
close(client_fd);
pthread_exit(NULL);
}
```
2. 客户端
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define SERVER_IP "127.0.0.1" // 服务器IP地址
#define SERVER_PORT 8888 // 服务器端口号
int main() {
int client_fd;
struct sockaddr_in server_addr;
char buffer[1024] = {0};
int ret;
// 创建客户端Socket
if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// 连接服务器
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
server_addr.sin_port = htons(SERVER_PORT);
if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("connect failed");
exit(EXIT_FAILURE);
}
printf("Client: connected to server, IP address: %s, port number: %d\n", SERVER_IP, SERVER_PORT);
// 发送消息给服务器
strcpy(buffer, "Hello, server!");
if (send(client_fd, buffer, strlen(buffer), 0) < 0) {
perror("send failed");
exit(EXIT_FAILURE);
}
// 接收服务器响应消息
memset(buffer, 0, sizeof(buffer));
if ((ret = recv(client_fd, buffer, sizeof(buffer), 0)) < 0) {
perror("recv failed");
exit(EXIT_FAILURE);
}
printf("Client: received message from server: %s\n", buffer);
// 关闭连接
close(client_fd);
return 0;
}
```
阅读全文