用C语言代码完成:这个简单的网络聊天室服务器支持多个客户端同时连接,并能够实现客户端之间的聊天功能。客户端连接到服务器后,会收到一个欢迎消息,然后可以发送消息给其他客户端。服务器会将接收到的消息广播给所有客户端。
时间: 2024-03-11 08:50:34 浏览: 52
Chat,一起看c语言源码,c语言程序
以下是一个简单的网络聊天室服务器的示例代码,使用C语言编写:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#define MAX_CLIENTS 10
// 定义客户端结构体
typedef struct {
int socket;
struct sockaddr_in address;
pthread_t thread;
} client_t;
// 定义客户端列表
client_t clients[MAX_CLIENTS];
// 定义互斥锁
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
// 定义服务器地址和端口号
#define PORT 8888
#define ADDRESS "127.0.0.1"
// 定义广播消息函数
void broadcast_message(char *message, int sender_socket) {
pthread_mutex_lock(&mutex);
for (int i = 0; i < MAX_CLIENTS; i++) {
if (clients[i].socket != 0 && clients[i].socket != sender_socket) {
send(clients[i].socket, message, strlen(message), 0);
}
}
pthread_mutex_unlock(&mutex);
}
// 定义客户端线程函数
void *client_thread(void *arg) {
client_t *client = (client_t *)arg;
printf("Client connected: %s:%d\n", inet_ntoa(client->address.sin_addr), ntohs(client->address.sin_port));
// 发送欢迎消息
char *welcome_message = "Welcome to the chatroom!\n";
send(client->socket, welcome_message, strlen(welcome_message), 0);
char message[1024];
while (1) {
// 接收客户端消息
memset(message, 0, sizeof(message));
int bytes_received = recv(client->socket, message, sizeof(message), 0);
if (bytes_received <= 0) {
break;
}
// 广播消息给其他客户端
broadcast_message(message, client->socket);
}
// 客户端断开连接
printf("Client disconnected: %s:%d\n", inet_ntoa(client->address.sin_addr), ntohs(client->address.sin_port));
close(client->socket);
client->socket = 0;
pthread_exit(NULL);
}
int main(int argc, char **argv) {
int server_socket;
struct sockaddr_in server_address;
// 创建socket对象
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
// 设置地址和端口号
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr(ADDRESS);
server_address.sin_port = htons(PORT);
// 绑定地址和端口号
if (bind(server_socket, (struct sockaddr *)&server_address, sizeof(server_address)) == -1) {
perror("bind");
exit(EXIT_FAILURE);
}
// 开始监听
if (listen(server_socket, MAX_CLIENTS) == -1) {
perror("listen");
exit(EXIT_FAILURE);
}
printf("Server started on %s:%d\n", ADDRESS, PORT);
while (1) {
// 等待客户端连接
struct sockaddr_in client_address;
socklen_t client_address_length = sizeof(client_address);
int client_socket = accept(server_socket, (struct sockaddr *)&client_address, &client_address_length);
if (client_socket == -1) {
perror("accept");
exit(EXIT_FAILURE);
}
// 查找空闲客户端
int index = -1;
for (int i = 0; i < MAX_CLIENTS; i++) {
if (clients[i].socket == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("Too many clients\n");
close(client_socket);
} else {
// 创建客户端线程
clients[index].socket = client_socket;
clients[index].address = client_address;
pthread_create(&clients[index].thread, NULL, client_thread, &clients[index]);
}
}
// 关闭socket
close(server_socket);
return 0;
}
```
这个简单的网络聊天室服务器支持多个客户端同时连接,并能够实现客户端之间的聊天功能。客户端连接到服务器后,会收到一个欢迎消息,然后可以发送消息给其他客户端。服务器会将接收到的消息广播给所有客户端。同时,服务器使用了互斥锁来保证线程安全,避免多个线程同时访问共享变量造成的数据竞争问题。
阅读全文