用C语言如何实现聊天室登录、注册、退出、添加好友、私聊、群聊的代码,分客户端和服务器,并具体注释给我
时间: 2023-10-20 13:10:26 浏览: 188
这是一个相对复杂的项目,需要多个文件的协作。以下是一个简单的服务器和客户端示例,包括登录、注册和退出功能,仅供参考。
服务器代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>
#define PORT 9000
#define MAX_CLIENTS 10
struct client_info {
int sockfd;
char username[20];
};
struct client_info clients[MAX_CLIENTS];
int client_count = 0;
// 处理客户端请求的线程函数
void *handle_client(void *arg) {
struct client_info *client = (struct client_info *) arg;
char buffer[1024];
sprintf(buffer, "Welcome, %s!\n", client->username);
write(client->sockfd, buffer, strlen(buffer));
while (1) {
memset(buffer, 0, sizeof(buffer));
int nbytes = read(client->sockfd, buffer, sizeof(buffer));
if (nbytes <= 0) {
printf("Client disconnected: %s\n", client->username);
close(client->sockfd);
break;
}
// 处理客户端请求
if (strncmp(buffer, "register", 8) == 0) {
char *username = strtok(buffer + 9, "\n");
strcpy(client->username, username);
write(client->sockfd, "Registered successfully!\n", 25);
} else if (strncmp(buffer, "login", 5) == 0) {
char *username = strtok(buffer + 6, "\n");
int i;
for (i = 0; i < client_count; i++) {
if (strcmp(clients[i].username, username) == 0) {
write(client->sockfd, "Login successfully!\n", 20);
break;
}
}
if (i == client_count) {
write(client->sockfd, "Login failed!\n", 14);
}
} else if (strncmp(buffer, "exit", 4) == 0) {
printf("Client disconnected: %s\n", client->username);
close(client->sockfd);
break;
} else {
write(client->sockfd, "Invalid command!\n", 17);
}
}
// 从客户端列表中移除
int i;
for (i = 0; i < client_count; i++) {
if (clients[i].sockfd == client->sockfd) {
break;
}
}
for (; i < client_count - 1; i++) {
clients[i] = clients[i + 1];
}
client_count--;
pthread_exit(NULL);
}
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;
// 创建套接字
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 = htonl(INADDR_ANY);
server_addr.sin_port = htons(PORT);
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 started on port %d\n", PORT);
while (1) {
// 接受连接
client_fd = accept(server_fd, (struct sockaddr *) &client_addr, &client_len);
if (client_fd < 0) {
perror("accept");
exit(1);
}
// 添加到客户端列表
if (client_count >= MAX_CLIENTS) {
printf("Too many clients!\n");
close(client_fd);
continue;
}
clients[client_count].sockfd = client_fd;
strcpy(clients[client_count].username, "anonymous");
client_count++;
// 创建线程处理请求
if (pthread_create(&tid, NULL, handle_client, &clients[client_count - 1]) != 0) {
perror("pthread_create");
exit(1);
}
}
return 0;
}
```
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 9000
int main(int argc, char *argv[]) {
int sockfd;
struct sockaddr_in server_addr;
char buffer[1024];
// 创建套接字
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("socket");
exit(1);
}
// 连接服务器
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
server_addr.sin_port = htons(PORT);
if (connect(sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) {
perror("connect");
exit(1);
}
// 处理用户输入
while (1) {
printf("> ");
fgets(buffer, sizeof(buffer), stdin);
write(sockfd, buffer, strlen(buffer));
memset(buffer, 0, sizeof(buffer));
int nbytes = read(sockfd, buffer, sizeof(buffer));
if (nbytes <= 0) {
printf("Server disconnected.\n");
break;
}
printf("%s", buffer);
if (strncmp(buffer, "exit", 4) == 0) {
break;
}
}
close(sockfd);
return 0;
}
```
以上代码仅供参考,实际应用中还需要添加好友、私聊、群聊等功能,以及对输入进行更详细的检查和处理。
阅读全文