C/C++ 编写一个服务器与客户端的群聊
时间: 2024-01-24 18:04:29 浏览: 79
以下是一个简单的基于TCP协议的服务器与客户端的群聊实现。该程序使用了多线程来支持多个客户端同时连接。
服务器代码(chat_server.c):
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#define MAX_CLIENTS 10
#define BUFFER_SIZE 1024
int clients[MAX_CLIENTS];
int num_clients = 0;
pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER;
void broadcast_message(char *message, int sender) {
pthread_mutex_lock(&clients_mutex);
for (int i = 0; i < num_clients; i++) {
if (clients[i] != sender) {
send(clients[i], message, strlen(message), 0);
}
}
pthread_mutex_unlock(&clients_mutex);
}
void *client_thread(void *arg) {
int client_fd = *(int*)arg;
char buffer[BUFFER_SIZE];
pthread_mutex_lock(&clients_mutex);
clients[num_clients++] = client_fd;
pthread_mutex_unlock(&clients_mutex);
while (1) {
int bytes_read = recv(client_fd, buffer, BUFFER_SIZE, 0);
if (bytes_read <= 0) {
pthread_mutex_lock(&clients_mutex);
for (int i = 0; i < num_clients; i++) {
if (clients[i] == client_fd) {
clients[i] = -1;
break;
}
}
pthread_mutex_unlock(&clients_mutex);
close(client_fd);
return NULL;
}
buffer[bytes_read] = '\0';
broadcast_message(buffer, client_fd);
}
}
int main(int argc, char *argv[]) {
int server_fd, client_fd;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len = sizeof(client_addr);
pthread_t tid;
if (argc != 2) {
printf("Usage: %s <port>\n", argv[0]);
exit(1);
}
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
perror("socket");
exit(1);
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(atoi(argv[1]));
if (bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("bind");
exit(1);
}
if (listen(server_fd, MAX_CLIENTS) < 0) {
perror("listen");
exit(1);
}
printf("Server listening on port %s...\n", argv[1]);
while (1) {
client_fd = accept(server_fd, (struct sockaddr*)&client_addr, &client_len);
if (client_fd < 0) {
perror("accept");
exit(1);
}
pthread_create(&tid, NULL, client_thread, &client_fd);
}
return 0;
}
```
客户端代码(chat_client.c):
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define BUFFER_SIZE 1024
int main(int argc, char *argv[]) {
int client_fd, port;
char buffer[BUFFER_SIZE];
struct hostent *server;
struct sockaddr_in server_addr;
if (argc != 3) {
printf("Usage: %s <hostname> <port>\n", argv[0]);
exit(1);
}
port = atoi(argv[2]);
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr, "Error: no such host\n");
exit(1);
}
client_fd = socket(AF_INET, SOCK_STREAM, 0);
if (client_fd < 0) {
perror("socket");
exit(1);
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
memcpy(&server_addr.sin_addr.s_addr, server->h_addr, server->h_length);
server_addr.sin_port = htons(port);
if (connect(client_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("connect");
exit(1);
}
printf("Connected to server %s:%d\n", argv[1], port);
while (1) {
printf("> ");
fgets(buffer, BUFFER_SIZE, stdin);
send(client_fd, buffer, strlen(buffer), 0);
}
return 0;
}
```
编译方法:
```
gcc -o chat_server chat_server.c -lpthread
gcc -o chat_client chat_client.c
```
运行方法:
```
./chat_server <port>
./chat_client <hostname> <port>
```
其中,`<port>`是服务器监听的端口号,`<hostname>`是服务器的主机名或IP地址。客户端连接服务器后,可以输入任意文本并发送到服务器,服务器会将该文本广播给所有连接的客户端。
阅读全文