Linux C语言tcp套接字多人聊天室,可实现私聊广播,编写服务器和客户端代码
时间: 2023-06-14 13:05:49 浏览: 225
服务器端代码:
```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
#define BUFFER_SIZE 1024
int client_count = 0; // 客户端数量
int client_sockets[MAX_CLIENTS]; // 客户端套接字
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // 互斥锁
// 发送消息给所有客户端,除了发送者
void broadcast(char *message, int sender_socket) {
pthread_mutex_lock(&mutex); // 加锁
for (int i = 0; i < client_count; i++) {
int socket_fd = client_sockets[i];
if (socket_fd != sender_socket) {
send(socket_fd, message, strlen(message), 0);
}
}
pthread_mutex_unlock(&mutex); // 解锁
}
// 发送消息给指定客户端
void send_to(char *message, int receiver_socket) {
send(receiver_socket, message, strlen(message), 0);
}
// 处理客户端消息
void *handle_client(void *arg) {
int client_socket = *(int *)arg;
char buffer[BUFFER_SIZE];
while (1) {
memset(buffer, 0, BUFFER_SIZE);
int bytes_received = recv(client_socket, buffer, BUFFER_SIZE, 0);
if (bytes_received <= 0) {
// 客户端断开连接
pthread_mutex_lock(&mutex);
for (int i = 0; i < client_count; i++) {
if (client_sockets[i] == client_socket) {
client_count--;
for (int j = i; j < client_count; j++) {
client_sockets[j] = client_sockets[j+1];
}
break;
}
}
pthread_mutex_unlock(&mutex);
close(client_socket);
break;
}
if (buffer[0] == '@') {
// 私聊消息
char *username = strtok(buffer+1, " ");
char *message = strtok(NULL, "\n");
int receiver_socket = -1;
pthread_mutex_lock(&mutex);
for (int i = 0; i < client_count; i++) {
if (strcmp(username, inet_ntoa(((struct sockaddr_in *)&client_sockets[i])->sin_addr)) == 0) {
receiver_socket = client_sockets[i];
break;
}
}
pthread_mutex_unlock(&mutex);
if (receiver_socket != -1) {
char response[BUFFER_SIZE];
sprintf(response, "(私聊)%s: %s", inet_ntoa(((struct sockaddr_in *)&client_socket)->sin_addr), message);
send_to(response, receiver_socket);
} else {
char response[BUFFER_SIZE];
sprintf(response, "用户 %s 不存在或不在线", username);
send_to(response, client_socket);
}
} else {
// 广播消息
char response[BUFFER_SIZE];
sprintf(response, "%s: %s", inet_ntoa(((struct sockaddr_in *)&client_socket)->sin_addr), buffer);
broadcast(response, client_socket);
}
}
return NULL;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <port>\n", argv[0]);
return -1;
}
int server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("socket");
return -1;
}
int port = atoi(argv[1]);
struct sockaddr_in server_addr;
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)) == -1) {
perror("bind");
close(server_socket);
return -1;
}
if (listen(server_socket, MAX_CLIENTS) == -1) {
perror("listen");
close(server_socket);
return -1;
}
pthread_t tid;
while (1) {
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
int client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_len);
if (client_socket == -1) {
perror("accept");
continue;
}
if (client_count == MAX_CLIENTS) {
printf("已达到最大客户端数量\n");
close(client_socket);
continue;
}
client_sockets[client_count++] = client_socket;
printf("客户端 %s:%d 连接成功\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
pthread_create(&tid, NULL, handle_client, &client_socket);
pthread_detach(tid);
}
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 sock_fd; // 客户端套接字
char username[20]; // 用户名
// 接收服务器消息
void *receive_message(void *arg) {
char buffer[BUFFER_SIZE];
while (1) {
memset(buffer, 0, BUFFER_SIZE);
int bytes_received = recv(sock_fd, buffer, BUFFER_SIZE, 0);
if (bytes_received <= 0) {
// 服务器断开连接
printf("与服务器断开连接\n");
close(sock_fd);
exit(0);
}
printf("%s\n", buffer);
}
return NULL;
}
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s <server_ip> <server_port>\n", argv[0]);
return -1;
}
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (sock_fd == -1) {
perror("socket");
return -1;
}
char *server_ip = argv[1];
int server_port = atoi(argv[2]);
struct sockaddr_in server_addr;
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(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("connect");
close(sock_fd);
return -1;
}
printf("请输入用户名: ");
scanf("%s", username);
pthread_t tid;
pthread_create(&tid, NULL, receive_message, NULL); // 创建一个线程接收服务器消息
pthread_detach(tid);
char buffer[BUFFER_SIZE];
while (1) {
memset(buffer, 0, BUFFER_SIZE);
fgets(buffer, BUFFER_SIZE, stdin);
if (buffer[0] == '\n') {
continue;
}
if (strcmp(buffer, "exit\n") == 0) {
break;
}
if (buffer[0] == '@') {
// 私聊消息
char *username = strtok(buffer+1, " ");
char *message = strtok(NULL, "\n");
if (message != NULL) {
char request[BUFFER_SIZE];
sprintf(request, "@%s %s\n", username, message);
send(sock_fd, request, strlen(request), 0);
}
} else {
// 广播消息
char request[BUFFER_SIZE];
sprintf(request, "%s\n", buffer);
send(sock_fd, request, strlen(request), 0);
}
}
close(sock_fd);
return 0;
}
```
阅读全文