Linux多人聊天室 消息队列和共享内存的代码
时间: 2024-01-01 20:04:44 浏览: 126
基于Linux的消息队列及多线程编程实现的聊天室
5星 · 资源好评率100%
实现一个 Linux 多人聊天室,可以使用消息队列和共享内存来实现消息的传递和存储,以下是示例代码:
1. 服务器端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <unistd.h>
#include <signal.h>
#define MAX_CLIENTS 10
#define MAX_MSG_SIZE 1024
#define SERVER_MSG_TYPE 1
#define CLIENT_MSG_TYPE 2
// 客户端信息结构体
typedef struct {
pid_t pid;
char nickname[20];
int msg_queue_id;
} client_info_t;
// 服务器消息结构体
typedef struct {
long type;
char msg[MAX_MSG_SIZE];
} server_msg_t;
// 客户端消息结构体
typedef struct {
long type;
char nickname[20];
char msg[MAX_MSG_SIZE];
} client_msg_t;
int msg_queue_id; // 消息队列ID
int shm_id; // 共享内存ID
client_info_t* clients; // 客户端信息数组
int num_clients = 0; // 当前客户端数量
// 信号处理函数
void signal_handler(int sig) {
if (sig == SIGINT) {
// 删除消息队列和共享内存
msgctl(msg_queue_id, IPC_RMID, NULL);
shmctl(shm_id, IPC_RMID, NULL);
exit(0);
}
}
// 广播消息
void broadcast(char* nickname, char* msg) {
int i;
client_msg_t client_msg;
server_msg_t server_msg;
// 将消息发送给每个客户端
for (i = 0; i < num_clients; i++) {
if (clients[i].pid == 0) {
continue;
}
// 将消息发送到客户端的消息队列中
client_msg.type = CLIENT_MSG_TYPE;
strcpy(client_msg.nickname, nickname);
strcpy(client_msg.msg, msg);
msgsnd(clients[i].msg_queue_id, &client_msg, sizeof(client_msg_t) - sizeof(long), 0);
}
// 将消息发送到服务器的消息队列中,以便记录聊天记录
server_msg.type = SERVER_MSG_TYPE;
sprintf(server_msg.msg, "[%s] %s", nickname, msg);
msgsnd(msg_queue_id, &server_msg, sizeof(server_msg_t) - sizeof(long), 0);
}
int main() {
int i;
pid_t pid;
client_msg_t client_msg;
server_msg_t server_msg;
// 注册信号处理函数
signal(SIGINT, signal_handler);
// 创建消息队列
msg_queue_id = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
if (msg_queue_id == -1) {
perror("msgget");
exit(1);
}
// 创建共享内存
shm_id = shmget(IPC_PRIVATE, sizeof(client_info_t) * MAX_CLIENTS, 0666 | IPC_CREAT);
if (shm_id == -1) {
perror("shmget");
exit(1);
}
clients = (client_info_t*) shmat(shm_id, NULL, 0);
if (clients == (void*) -1) {
perror("shmat");
exit(1);
}
// 初始化客户端信息数组
for (i = 0; i < MAX_CLIENTS; i++) {
clients[i].pid = 0;
clients[i].msg_queue_id = 0;
strcpy(clients[i].nickname, "");
}
while (1) {
// 接收客户端消息
if (msgrcv(msg_queue_id, &client_msg, sizeof(client_msg_t) - sizeof(long), CLIENT_MSG_TYPE, 0) == -1) {
perror("msgrcv");
continue;
}
// 处理客户端消息
if (strcmp(client_msg.msg, "/exit") == 0) {
// 客户端退出
for (i = 0; i < num_clients; i++) {
if (strcmp(clients[i].nickname, client_msg.nickname) == 0) {
clients[i].pid = 0;
clients[i].msg_queue_id = 0;
strcpy(clients[i].nickname, "");
num_clients--;
break;
}
}
printf("[Server] User %s left the chat room.\n", client_msg.nickname);
broadcast("Server", client_msg.msg);
} else if (client_msg.msg[0] == '/') {
// 处理客户端命令
char* cmd = strtok(client_msg.msg, " ");
if (strcmp(cmd, "/list") == 0) {
// 显示在线用户列表
printf("[Server] Online users:\n");
for (i = 0; i < num_clients; i++) {
if (clients[i].pid != 0) {
printf("[Server] %s\n", clients[i].nickname);
}
}
} else {
// 未知命令
server_msg.type = SERVER_MSG_TYPE;
sprintf(server_msg.msg, "[Server] Unknown command: %s", cmd);
msgsnd(client_msg.msg_queue_id, &server_msg, sizeof(server_msg_t) - sizeof(long), 0);
}
} else {
// 广播消息
broadcast(client_msg.nickname, client_msg.msg);
}
// 处理退出的客户端进程
for (i = 0; i < num_clients; i++) {
if (clients[i].pid != 0 && kill(clients[i].pid, 0) == -1) {
clients[i].pid = 0;
clients[i].msg_queue_id = 0;
strcpy(clients[i].nickname, "");
num_clients--;
printf("[Server] User %d disconnected.\n", i);
broadcast("Server", "User disconnected.");
}
}
// 接受新的客户端连接
if (num_clients < MAX_CLIENTS) {
if (msgrcv(msg_queue_id, &client_msg, sizeof(client_msg_t) - sizeof(long), CLIENT_MSG_TYPE, IPC_NOWAIT) != -1) {
if (strcmp(client_msg.msg, "/join") == 0) {
// 客户端连接
for (i = 0; i < MAX_CLIENTS; i++) {
if (clients[i].pid == 0) {
pid = fork();
if (pid == -1) {
perror("fork");
} else if (pid == 0) {
// 子进程处理客户端连接
execl("./client", "client", NULL);
perror("execl");
exit(1);
} else {
// 父进程保存客户端信息
clients[i].pid = pid;
clients[i].msg_queue_id = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
if (clients[i].msg_queue_id == -1) {
perror("msgget");
exit(1);
}
strcpy(clients[i].nickname, client_msg.nickname);
num_clients++;
printf("[Server] User %s joined the chat room.\n", client_msg.nickname);
broadcast("Server", "User joined the chat room.");
client_msg.type = CLIENT_MSG_TYPE;
sprintf(client_msg.msg, "Welcome to the chat room, %s!", client_msg.nickname);
msgsnd(clients[i].msg_queue_id, &client_msg, sizeof(client_msg_t) - sizeof(long), 0);
break;
}
}
}
}
}
}
}
return 0;
}
```
2. 客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>
#include <signal.h>
#define MAX_MSG_SIZE 1024
#define SERVER_MSG_TYPE 1
#define CLIENT_MSG_TYPE 2
// 服务器消息结构体
typedef struct {
long type;
char msg[MAX_MSG_SIZE];
} server_msg_t;
// 客户端消息结构体
typedef struct {
long type;
char nickname[20];
char msg[MAX_MSG_SIZE];
} client_msg_t;
int msg_queue_id; // 消息队列ID
char nickname[20]; // 用户昵称
// 信号处理函数
void signal_handler(int sig) {
if (sig == SIGINT) {
// 发送退出消息
client_msg_t client_msg;
client_msg.type = CLIENT_MSG_TYPE;
strcpy(client_msg.nickname, nickname);
strcpy(client_msg.msg, "/exit");
msgsnd(msg_queue_id, &client_msg, sizeof(client_msg_t) - sizeof(long), 0);
exit(0);
}
}
int main() {
int i;
pid_t pid;
client_msg_t client_msg;
server_msg_t server_msg;
// 注册信号处理函数
signal(SIGINT, signal_handler);
// 创建消息队列
msg_queue_id = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
if (msg_queue_id == -1) {
perror("msgget");
exit(1);
}
// 向服务器发送连接请求
client_msg.type = CLIENT_MSG_TYPE;
printf("Please input your nickname: ");
scanf("%s", nickname);
strcpy(client_msg.nickname, nickname);
strcpy(client_msg.msg, "/join");
msgsnd(msg_queue_id, &client_msg, sizeof(client_msg_t) - sizeof(long), 0);
// 接收服务器消息
while (1) {
if (msgrcv(msg_queue_id, &server_msg, sizeof(server_msg_t) - sizeof(long), SERVER_MSG_TYPE, 0) == -1) {
perror("msgrcv");
continue;
}
printf("%s\n", server_msg.msg);
}
return 0;
}
```
以上代码只是一个简单的示例,实际上还有很多细节需要考虑,比如如何避免重名、如何处理客户端连接异常等。
阅读全文