在Linux环境下用C写多人聊天室
时间: 2024-02-15 12:02:39 浏览: 84
好的,下面就是一个简单的基于Linux环境下使用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 100
#define BUFFER_SIZE 1024
int clients[MAX_CLIENTS];
int current_client_count = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void broadcast(int sender, char* message) {
pthread_mutex_lock(&mutex);
for (int i = 0; i < current_client_count; i++) {
if (clients[i] != sender) {
send(clients[i], message, strlen(message), 0);
}
}
pthread_mutex_unlock(&mutex);
}
void *handle_client(void *arg) {
int client_socket = *(int*)arg;
char buffer[BUFFER_SIZE];
int read_size;
while ((read_size = recv(client_socket, buffer, BUFFER_SIZE, 0)) > 0) {
buffer[read_size] = '\0';
broadcast(client_socket, buffer);
}
pthread_mutex_lock(&mutex);
for (int i = 0; i < current_client_count; i++) {
if (clients[i] == client_socket) {
while (i < current_client_count - 1) {
clients[i] = clients[i + 1];
i++;
}
break;
}
}
current_client_count--;
pthread_mutex_unlock(&mutex);
printf("Client %d disconnected\n", client_socket);
close(client_socket);
pthread_exit(NULL);
}
int main(int argc, char const *argv[]) {
int server_socket, client_socket, port;
struct sockaddr_in server_address, client_address;
pthread_t thread_id;
if (argc < 2) {
printf("Usage: %s <port>\n", argv[0]);
return -1;
}
port = atoi(argv[1]);
if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Error creating socket");
return -1;
}
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(port);
if (bind(server_socket, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) {
perror("Error binding socket");
return -1;
}
if (listen(server_socket, MAX_CLIENTS) < 0) {
perror("Error listening on socket");
return -1;
}
printf("Server listening on port %d...\n", port);
while (1) {
socklen_t client_address_size = sizeof(client_address);
if ((client_socket = accept(server_socket, (struct sockaddr*)&client_address, &client_address_size)) < 0) {
perror("Error accepting connection");
continue;
}
pthread_mutex_lock(&mutex);
if (current_client_count >= MAX_CLIENTS) {
printf("Maximum client count reached, rejecting connection from %s:%d\n",
inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port));
close(client_socket);
pthread_mutex_unlock(&mutex);
continue;
}
clients[current_client_count++] = client_socket;
pthread_mutex_unlock(&mutex);
printf("Client %d connected from %s:%d\n", client_socket,
inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port));
pthread_create(&thread_id, NULL, handle_client, (void*)&client_socket);
pthread_detach(thread_id);
}
close(server_socket);
return 0;
}
```
客户端代码:
```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 BUFFER_SIZE 1024
int socket_descriptor;
pthread_t receive_thread_id;
void *receive_message(void *arg) {
char buffer[BUFFER_SIZE];
int read_size;
while ((read_size = recv(socket_descriptor, buffer, BUFFER_SIZE, 0)) > 0) {
buffer[read_size] = '\0';
printf("%s", buffer);
}
printf("Disconnected from server\n");
close(socket_descriptor);
pthread_exit(NULL);
}
int main(int argc, char const *argv[]) {
char* server_address_str;
int port;
struct sockaddr_in server_address;
char message[BUFFER_SIZE];
if (argc < 3) {
printf("Usage: %s <server-address> <port>\n", argv[0]);
return -1;
}
server_address_str = argv[1];
port = atoi(argv[2]);
if ((socket_descriptor = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Error creating socket");
return -1;
}
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr(server_address_str);
server_address.sin_port = htons(port);
if (connect(socket_descriptor, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) {
perror("Error connecting to server");
return -1;
}
printf("Connected to server %s:%d\n", server_address_str, port);
pthread_create(&receive_thread_id, NULL, receive_message, NULL);
while (1) {
fgets(message, BUFFER_SIZE, stdin);
send(socket_descriptor, message, strlen(message), 0);
}
close(socket_descriptor);
return 0;
}
```
这是一个非常简单的实现,仅供参考。在实际应用中,还需要考虑更多的细节,例如消息格式、消息队列、异常处理等等。
阅读全文