linux聊天室程序代码c语言代码 约600行
时间: 2024-07-23 15:01:08 浏览: 83
linux下的简单聊天室源代码(c语言实现)
4星 · 用户满意度95%
编写一个完整的Linux聊天室程序大约需要几百行代码,这取决于是否使用了库如socket、pthread或多线程等。下面是一个简单的示例,展示了一个基于C语言的基本TCP聊天室服务器和客户端的概念,但这只是一个基础框架,并非完整实现。
**服务器端(server.c)**
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define MAX_CLIENTS 5
#define BUFFER_SIZE 1024
int main(int argc, char *argv[]) {
int server_fd, client_sock, c;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_len = sizeof(client_addr);
pthread_t client_thread[MAX_CLIENTS];
if (argc != 2) {
printf("Usage: %s <port>\n", argv);
return -1;
}
// 创建服务器套接字
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv));
server_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Error binding socket");
close(server_fd);
exit(EXIT_FAILURE);
}
listen(server_fd, MAX_CLIENTS);
while (1) {
printf("Waiting for clients...\n");
// 接受连接
if ((client_sock = accept(server_fd, (struct sockaddr *)&client_addr, &addr_len)) < 0) {
perror("Error accepting connection");
continue;
}
// 启动新的线程处理每个客户端
pthread_create(&client_thread[c], NULL, handle_client, (void *)client_sock);
c++;
}
return 0;
}
// 客户端处理函数
void* handle_client(void* client_sock_ptr) {
int client_sock = *(int*)client_sock_ptr;
char buffer[BUFFER_SIZE];
while (1) {
read(client_sock, buffer, BUFFER_SIZE - 1);
buffer[BUFFER_SIZE - 1] = '\0';
printf("Received message from client: %s\n", buffer);
send(client_sock, "Server received!", strlen("Server received!"), 0);
if (strcmp(buffer, "exit") == 0) break;
}
close(client_sock);
return NULL;
}
```
**客户端(client.c)**
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s <hostname> <port>\n", argv);
return -1;
}
int client_sock;
struct sockaddr_in server_addr;
char message[BUFFER_SIZE];
client_sock = socket(AF_INET, SOCK_STREAM, 0);
if (client_sock == -1) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv));
inet_pton(AF_INET, argv, &server_addr.sin_addr);
if (connect(client_sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Error connecting to server");
close(client_sock);
exit(EXIT_FAILURE);
}
while (1) {
printf("Enter your message or 'exit' to quit: ");
fgets(message, BUFFER_SIZE, stdin);
message[strcspn(message, "\n")] = '\0';
if (strcmp(message, "exit") == 0) break;
write(client_sock, message, strlen(message));
}
close(client_sock);
return 0;
}
```
要运行这个简单的聊天室程序,首先编译服务器端(`gcc server.c -o server`),然后启动服务器并等待客户端连接。接着编译客户端(`gcc client.c -o client`),并连接到服务器。
请注意,这只是一个基本示例,实际应用中可能需要考虑更多的错误处理、用户认证、数据持久化等。
阅读全文