server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/epoll.h>
#define MAX_FD_NUM 3
void setnonblock(int fd) {
int flag = fcntl(fd, F_GETFL, 0);
if (flag == -1) {
printf("get fcntl flag %s\n", strerror(errno));
return;
}
int ret = fcntl(fd, F_SETFL, flag | O_NONBLOCK);
if (ret == -1) {
printf("set fcntl non-blocking %s\n", strerror(errno));
return;
}
}
int socket_create(int port) {
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd == -1) {
printf("socket create %s\n", strerror(errno));
return -1;
}
setnonblock(fd);
st