1、服务器端实现
服务器端的主要功能是监听客户端连接,并将一个客户端发送的消息广播给所有连接的客户端。使用 socket()
创建套接字并绑定到指定端口。使用 accept()
等待客户端连接,每个连接创建一个线程来处理客户端。客户端发送的消息会被广播给所有其他已连接的客户端。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>
#define PORT 8080
#define MAX_CLIENTS 10
#define BUFFER_SIZE 1024
int client_sockets[MAX_CLIENTS];
pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER;
void *handle_client(void *socket_desc) {
int sock = *(int *)socket_desc;
char buffer[BUFFER_SIZE];
int read_size;
while ((read_size = recv(sock, buffer, BUFFER_SIZE, 0)) > 0) {
buffer[read_size] = '\0';
printf("Client %d: %s\n", sock, buffer);
// 广播消息给其他客户端
pthread_mutex_lock(&clients_mutex);
for (int i = 0; i < MAX_CLIENTS; i++) {
if (client_sockets[i] != 0 && client_sockets[i] != sock) {
send(client_sockets[i], buffer, strlen(buffer), 0);
}
}
pthread_mutex_unlock(&clients_mutex);
}
// 客户端断开连接
pthread_mutex_lock(&clients_mutex);
for (int i = 0; i < MAX_CLIENTS; i++) {
if (client_sockets[i] == sock) {
client_sockets[i] = 0;
break;
}
}
pthread_mutex_unlock(&clients_mutex);
close(sock);
free(socket_desc);
return 0;
}
int main() {
int server_socket, client_socket, *new_sock;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len = sizeof(struct sockaddr_in);
// 创建服务器套接字
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
printf("Could not create socket\n");
return 1;
}
// 设置服务器地址
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
// 绑定套接字到地址
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Bind failed");
return 1;
}
listen(server_socket, MAX_CLIENTS);
printf("Server started on port %d\n", PORT);
while ((client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_len))) {
printf("Client connected: %d\n", client_socket);
// 添加客户端到数组
pthread_mutex_lock(&clients_mutex);
for (int i = 0; i < MAX_CLIENTS; i++) {
if (client_sockets[i] == 0) {
client_sockets[i] = client_socket;
break;
}
}
pthread_mutex_unlock(&clients_mutex);
// 为每个客户端创建一个新线程
pthread_t client_thread;
new_sock = malloc(sizeof(int));
*new_sock = client_socket;
if (pthread_create(&client_thread, NULL, handle_client, (void *)new_sock) < 0) {
perror("Could not create thread");
return 1;
}
pthread_detach(client_thread);
}
if (client_socket < 0) {
perror("Accept failed");
return 1;
}
close(server_socket);
return 0;
}
2、客户端实现
客户端的主要功能是连接到服务器,并发送和接收消息。客户端首先需要连接到服务器,然后进入一个循环,等待用户输入消息并发送到服务器,同时接收并显示来自服务器的消息。使用 socket()
创建套接字并连接到服务器。使用一个线程不断接收服务器发送的消息,并在主线程中发送消息给服务器。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>
#define PORT 8080
#define BUFFER_SIZE 1024
void *receive_messages(void *socket_desc) {
int sock = *(int *)socket_desc;
char buffer[BUFFER_SIZE];
int read_size;
while ((read_size = recv(sock, buffer, BUFFER_SIZE, 0)) > 0) {
buffer[read_size] = '\0';
printf("%s", buffer);
}
return 0;
}
int main() {
int client_socket;
struct sockaddr_in server_addr;
char message[BUFFER_SIZE];
pthread_t receive_thread;
// 创建客户端套接字
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
printf("Could not create socket\n");
return 1;
}
// 设置服务器地址
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
// 连接到服务器
if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Connect failed");
return 1;
}
printf("Connected to server\n");
// 创建接收消息的线程
if (pthread_create(&receive_thread, NULL, receive_messages, (void *)&client_socket) < 0) {
perror("Could not create thread");
return 1;
}
// 发送消息到服务器
while (fgets(message, BUFFER_SIZE, stdin) != NULL) {
send(client_socket, message, strlen(message), 0);
}
close(client_socket);
return 0;
}
3、编译运行
1)先编译运行服务器程序
gcc server.c -o server -pthread
./server
2)再编译运行多个客户端程序
gcc client.c -o client -pthread
./client