计算机网络课设要求做一个基于TCP协议的简易聊天机器人代码要求可多个客户端
时间: 2024-01-24 21:01:46 浏览: 81
好的,下面我将给出一个基于TCP协议的简易聊天机器人C语言示例代码,可以同时与多个客户端进行交互,希望能帮到您:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#define PORT 8888
#define MAX_MSG_SIZE 1024
#define MAX_CLIENTS 10
void *handle_client(void *arg);
int main()
{
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[MAX_MSG_SIZE] = {0};
char *hello = "Hello from server";
pthread_t thread_id;
int i, client_sockets[MAX_CLIENTS], max_clients = MAX_CLIENTS, activity, sd, max_sd;
fd_set readfds;
// 创建TCP套接字
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
// 设置套接字选项
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
// 初始化服务器地址
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// 绑定地址和端口
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
// 监听连接
if (listen(server_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
// 初始化客户端套接字列表
for (i = 0; i < max_clients; i++)
{
client_sockets[i] = 0;
}
// 循环等待客户端连接和消息
while (1)
{
// 清空文件描述符集合
FD_ZERO(&readfds);
// 将服务器套接字加入文件描述符集合
FD_SET(server_fd, &readfds);
max_sd = server_fd;
// 将客户端套接字加入文件描述符集合
for (i = 0; i < max_clients; i++)
{
sd = client_sockets[i];
if (sd > 0)
FD_SET(sd, &readfds);
if (sd > max_sd)
max_sd = sd;
}
// 等待读取文件描述符集合中的套接字
activity = select(max_sd + 1, &readfds, NULL, NULL, NULL);
if ((activity < 0) && (errno != EINTR))
{
printf("select error");
}
// 如果服务器套接字有新的连接请求
if (FD_ISSET(server_fd, &readfds))
{
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
// 发送欢迎消息
send(new_socket, hello, strlen(hello), 0);
printf("Hello message sent\n");
// 将新连接的客户端套接字加入列表中
for (i = 0; i < max_clients; i++)
{
if (client_sockets[i] == 0)
{
client_sockets[i] = new_socket;
printf("Adding to list of sockets as %d\n" , i);
break;
}
}
}
// 处理客户端消息
for (i = 0; i < max_clients; i++)
{
sd = client_sockets[i];
if (FD_ISSET(sd, &readfds))
{
// 读取消息
if ((valread = read(sd, buffer, MAX_MSG_SIZE)) == 0)
{
getpeername(sd, (struct sockaddr*)&address, (socklen_t*)&addrlen);
printf("Host disconnected , ip %s , port %d \n" , inet_ntoa(address.sin_addr) , ntohs(address.sin_port));
// 关闭套接字并从列表中移除
close(sd);
client_sockets[i] = 0;
}
else
{
// 启动新线程处理客户端消息
pthread_create(&thread_id, NULL, handle_client, (void *)&sd);
pthread_detach(thread_id);
}
}
}
}
// 关闭套接字
close(server_fd);
return 0;
}
void *handle_client(void *arg)
{
int sd = *(int*)arg;
char buffer[MAX_MSG_SIZE] = {0};
// 读取客户端消息
read(sd, buffer, MAX_MSG_SIZE);
// 根据客户端发送的消息,给出回复
if (strcmp(buffer, "Hello\n") == 0)
{
printf("Server: Hi there!\n");
send(sd, "Hi there!", strlen("Hi there!"), 0);
}
else if (strcmp(buffer, "How are you?\n") == 0)
{
printf("Server: I'm fine, thank you!\n");
send(sd, "I'm fine, thank you!", strlen("I'm fine, thank you!"), 0);
}
else if (strcmp(buffer, "Quit\n") == 0)
{
printf("Server: Goodbye!\n");
send(sd, "Goodbye!", strlen("Goodbye!"), 0);
}
else
{
printf("Server: Sorry, I don't understand.\n");
send(sd, "Sorry, I don't understand.", strlen("Sorry, I don't understand."), 0);
}
// 关闭套接字
close(sd);
return NULL;
}
```
这个示例代码创建了一个TCP服务器程序,等待客户端连接和消息,并根据预先设定好的回复规则,与客户端进行交互。当收到客户端发送的消息后,服务器会根据预先设定好的回复规则,给客户端发送回复。当客户端发送“Quit”消息时,服务器会关闭连接。在实际应用中,可以根据需要修改回复规则和相关参数。同时,这个示例代码使用了多线程技术,可以同时处理多个客户端的消息。
阅读全文