基于linu的聊天室系统代码
时间: 2023-12-14 08:02:20 浏览: 68
好的,这里提供一个基于 Linux 的简单聊天室系统代码示例,供您参考:
### 服务器端代码
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#define MAX_CLIENTS 10
#define BUFFER_SIZE 1024
int client_sockets[MAX_CLIENTS] = {0};
int num_clients = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *handle_client(void *arg) {
int client_socket = *(int *)arg;
char buffer[BUFFER_SIZE] = {0};
int read_size = 0;
while ((read_size = recv(client_socket, buffer, BUFFER_SIZE, 0)) > 0) {
pthread_mutex_lock(&mutex);
for (int i = 0; i < num_clients; i++) {
if (client_sockets[i] != client_socket) {
send(client_sockets[i], buffer, read_size, 0);
}
}
pthread_mutex_unlock(&mutex);
memset(buffer, 0, BUFFER_SIZE);
}
pthread_mutex_lock(&mutex);
for (int i = 0; i < num_clients; i++) {
if (client_sockets[i] == client_socket) {
client_sockets[i] = 0;
num_clients--;
break;
}
}
pthread_mutex_unlock(&mutex);
close(client_socket);
pthread_exit(NULL);
}
int main(int argc, char *argv[]) {
int server_socket = 0;
int port = 8888;
struct sockaddr_in server_addr;
pthread_t thread_id;
// 创建服务器 socket
if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(1);
}
// 设置服务器地址和端口
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(port);
// 绑定服务器地址和端口
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("bind");
exit(1);
}
// 监听客户端连接请求
if (listen(server_socket, MAX_CLIENTS) < 0) {
perror("listen");
exit(1);
}
// 接收客户端连接请求并处理
while (1) {
int client_socket = 0;
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
if ((client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_len)) < 0) {
perror("accept");
exit(1);
}
if (num_clients >= MAX_CLIENTS) {
printf("Maximum clients reached. Connection rejected.\n");
close(client_socket);
continue;
}
client_sockets[num_clients] = client_socket;
num_clients++;
pthread_create(&thread_id, NULL, handle_client, (void *)&client_socket);
}
close(server_socket);
return 0;
}
```
### 客户端代码
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#define BUFFER_SIZE 1024
int client_socket = 0;
void *handle_recv(void *arg) {
char buffer[BUFFER_SIZE] = {0};
int read_size = 0;
while ((read_size = recv(client_socket, buffer, BUFFER_SIZE, 0)) > 0) {
printf("%s", buffer);
memset(buffer, 0, BUFFER_SIZE);
}
close(client_socket);
pthread_exit(NULL);
}
int main(int argc, char *argv[]) {
char *server_ip = "127.0.0.1";
int server_port = 8888;
struct sockaddr_in server_addr;
pthread_t thread_id;
// 创建客户端 socket
if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(1);
}
// 设置服务器地址和端口
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(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("connect");
exit(1);
}
// 创建接收线程
pthread_create(&thread_id, NULL, handle_recv, NULL);
// 发送消息
char buffer[BUFFER_SIZE] = {0};
int read_size = 0;
while ((read_size = read(STDIN_FILENO, buffer, BUFFER_SIZE)) > 0) {
send(client_socket, buffer, read_size, 0);
memset(buffer, 0, BUFFER_SIZE);
}
close(client_socket);
return 0;
}
```
以上示例代码仅供参考,实际使用时需要根据实际情况进行调整和优化。同时,需要注意数据传输的安全性问题,例如使用 SSL/TLS 等加密协议来保护数据传输的安全性。
阅读全文