一步一步创建聊天程序2-利用epoll来创建简单的聊天室
如图,这个是看视频时,最后的作业,除了客户端未使用select实现外,其它的要求都有简单实现。 服务端代码如下: #include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <errno.h> #include <sys/epoll.h> #include <netinet/in.h> #include <unistd.h> #include<netinet/tcp.h> #define MAX_LISTEN 10 #define EPOLL_SIZE 100 struct message { int target_id; char buf[100]; }; struct user_password { char username[100]; char password[100]; }; //设置保活参数 keepalive_time保活时间,keepalive_intvl保活间隔,keepalive_probes保活探测数。 int set_keepalive(int sockfd,int keepalive_time, int keepalive_intvl, int keepalive_probes) { int optval; socklen_t