tcp聊天室私聊群聊c
时间: 2024-01-09 09:22:10 浏览: 90
以下是一个基于TCP的聊天室的私聊和群聊功能的C代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define MAX_CLIENTS 10
#define BUFFER_SIZE 1024
int main() {
int serverSocket, clientSockets[MAX_CLIENTS];
struct sockaddr_in serverAddress, clientAddress;
char buffer[BUFFER_SIZE];
// 创建服务器套接字
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == -1) {
perror("Failed to create socket");
exit(EXIT_FAILURE);
}
// 设置服务器地址和端口
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = INADDR_ANY;
serverAddress.sin_port = htons(8888);
// 绑定服务器套接字到指定地址和端口
if (bind(serverSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) < 0) {
perror("Failed to bind socket");
exit(EXIT_FAILURE);
}
// 监听连接请求
if (listen(serverSocket, MAX_CLIENTS) < 0) {
perror("Failed to listen for connections");
exit(EXIT_FAILURE);
}
printf("Server started. Waiting for connections...\n");
fd_set readfds;
int maxSd, activity, newSocket, i, valread, sd;
int maxClients = MAX_CLIENTS;
int clientSd;
int clientConnected[MAX_CLIENTS] = {0};
char *message = "Welcome to the chat room!\n";
while (1) {
// 清空文件描述符集合
FD_ZERO(&readfds);
// 添加服务器套接字到文件描述符集合
FD_SET(serverSocket, &readfds);
maxSd = serverSocket;
// 添加客户端套接字到文件描述符集合
for (i = 0; i < maxClients; i++) {
sd = clientSockets[i];
if (sd > 0) {
FD_SET(sd, &readfds);
}
if (sd > maxSd) {
maxSd = sd;
}
}
// 等待活动套接字
activity = select(maxSd + 1, &readfds, NULL, NULL, NULL);
if ((activity < 0) && (errno != EINTR)) {
printf("select error");
}
// 如果有新的连接请求
if (FD_ISSET(serverSocket, &readfds)) {
if ((newSocket = accept(serverSocket, (struct sockaddr *)&clientAddress, (socklen_t *)&clientAddress)) < 0) {
perror("Failed to accept connection");
exit(EXIT_FAILURE);
}
printf("New connection, socket fd is %d, ip is : %s, port : %d\n", newSocket, inet_ntoa(clientAddress.sin_addr), ntohs(clientAddress.sin_port));
// 发送欢迎消息给新连接的客户端
if (send(newSocket, message, strlen(message), 0) != strlen(message)) {
perror("Failed to send welcome message");
}
// 添加新的套接字到客户端套接字数组
for (i = 0; i < maxClients; i++) {
if (clientSockets[i] == 0) {
clientSockets[i] = newSocket;
printf("Adding to list of sockets as %d\n", i);
break;
}
}
}
// 处理客户端的消息
for (i = 0; i < maxClients; i++) {
clientSd = clientSockets[i];
if (FD_ISSET(clientSd, &readfds)) {
// 读取客户端的消息
valread = read(clientSd, buffer, BUFFER_SIZE);
buffer[valread] = '\0';
// 判断客户端是否断开连接
if (valread == 0) {
getpeername(clientSd, (struct sockaddr *)&clientAddress, (socklen_t *)&clientAddress);
printf("Host disconnected, ip %s, port %d\n", inet_ntoa(clientAddress.sin_addr), ntohs(clientAddress.sin_port));
// 关闭客户端套接字并从数组中移除
close(clientSd);
clientSockets[i] = 0;
} else {
// 判断客户端的消息类型
if (strncmp(buffer, "获取用户列表", 6) == 0) {
// 发送在线用户列表给客户端
char userList[BUFFER_SIZE] = "在线用户列表:\n";
for (int j = 0; j < maxClients; j++) {
if (clientSockets[j] != 0) {
strcat(userList, inet_ntoa(clientAddress.sin_addr));
strcat(userList, "\n");
}
}
send(clientSd, userList, strlen(userList), 0);
} else if (strncmp(buffer, "私聊", 2) == 0) {
// 解析私聊目标用户名
char *targetUsername = strtok(buffer, " ");
targetUsername = strtok(NULL, " ");
// 解析私聊消息内容
char *messageContent = strtok(NULL, "");
// 查找目标用户的套接字
int targetSd = -1;
for (int j = 0; j < maxClients; j++) {
if (clientSockets[j] != 0 && strcmp(inet_ntoa(clientAddress.sin_addr), targetUsername) == 0) {
targetSd = clientSockets[j];
break;
}
}
// 发送私聊消息给目标用户
if (targetSd != -1) {
char privateMessage[BUFFER_SIZE];
sprintf(privateMessage, "来自 %s 的私聊消息: %s", inet_ntoa(clientAddress.sin_addr), messageContent);
send(targetSd, privateMessage, strlen(privateMessage), 0);
} else {
char errorMessage[BUFFER_SIZE] = "目标用户不存在或不在线";
send(clientSd, errorMessage, strlen(errorMessage), 0);
}
} else if (strncmp(buffer, "退出私聊", 6) == 0) {
// 退出私聊模式
char exitMessage[BUFFER_SIZE] = "已退出私聊模式";
send(clientSd, exitMessage, strlen(exitMessage), 0);
} else {
// 群聊消息,将消息发送给所有在线用户
for (int j = 0; j < maxClients; j++) {
if (clientSockets[j] != 0 && clientSockets[j] != clientSd) {
send(clientSockets[j], buffer, strlen(buffer), 0);
}
}
}
}
}
}
}
return 0;
}
```
阅读全文