#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
int client(int connfd)
{
int ret;
char buf[256];
memset(buf, 0, sizeof(buf));
ret = read(connfd, buf, sizeof(buf));
if (ret == -1) {
perror("server->read");
return -1;
} else if (ret == 0) {
return -1;
}
printf("buf : %s\n", buf);
ret = write(connfd, buf, sizeof(buf));
if (ret == -1) {
perror("server->write");
return -1;
}
return 0;
}
int main(int argc, char *argv[])
{
int listenfd;
int ret;
socklen_t addrlen;
int connfd;
pid_t pid;
char buf[256];
struct sockaddr_in srvaddr;
struct sockaddr_in cltaddr;
/* 1. 创建服务器(创建一socket套接字);socket */
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd == -1) {
perror("server->socket");
return -1;
}
printf("create listenfd = %d success\n", listenfd);
int opt = 1;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
/* 2. 设置服务器的IP地址和端口号(将socket和服务器的IP地址和端口号进行绑定);bind */
memset(&srvaddr, 0, sizeof(struct sockaddr_in));
srvaddr.sin_family = AF_INET;
srvaddr.sin_port = htons(9999);
srvaddr.sin_addr.s_addr = htonl(INADDR_ANY);
ret = bind(listenfd, (const struct sockaddr *)&srvaddr, sizeof(srvaddr));
printf("port : %d\n", ntohs(srvaddr.sin_port));
if (ret == -1) {
perror("server->bind");
return -1;
}
printf("bind success !\n");
/* 3. 启动监听(启动服务器); listen */
ret = listen(listenfd, 1024);
if (ret == -1) {
perror("server->listen");
return -1;
}
printf("listen success !\n");
/* 创建集合;*/
int epfd;
int i;
int fd;
epfd = epoll_create(1024);
if (epfd == -1) {
perror("epoll_create");
return -1;
}
printf("epfd = %d\n", epfd);
/* 添加所需要处理的事件和文件描述符 */
struct epoll_event event;
event.events = EPOLLIN;
event.data.fd = listenfd;
ret = epoll_ctl(epfd, EPOLL_CTL_ADD, listenfd, &event);
if (ret == -1) {
perror("epoll_ctl->EPOLL_CTL_ADD");
return -1;
}
struct epoll_event events[1024];
while(1) {
/* 检测集合当中是否有继续的文件描述符 */
ret = epoll_wait(epfd, events, 1024, 5000);
if (ret == -1) {
perror("poll");
return -1;
} else if (ret == 0) {
printf("timeout\n");
continue;
}
/* 轮循 */
for (i = 0; i < ret; i++) {
/* 判断是什么事件 */
if (EPOLLIN == events[i].events) {
fd = events[i].data.fd;
/* 如果是监听套接字listenfd,则建立连接 */
if (fd == listenfd) {
memset(&cltaddr, 0, sizeof(cltaddr));
addrlen = sizeof(socklen_t);
connfd = accept(listenfd, (struct sockaddr *)&cltaddr, &addrlen);
if (connfd == -1) {
perror("accept");
return -1;
}
printf("connfd = %d\n", connfd);
event.events = EPOLLIN;
event.data.fd = connfd;
ret = epoll_ctl(epfd, EPOLL_CTL_ADD, connfd, &event);
if (ret == -1) {
perror("epoll_ctl->EPOLL_CTL_ADD");
return -1;
}
} else {
ret = client(fd);
if (ret == -1) {
ret = epoll_ctl(epfd, EPOLL_CTL_DEL, fd, &event);
if (ret == -1) {
perror("epoll_ctl->EPOLL_CTL_DEL");
return -1;
}
close(fd);
}
}
}
}
}
close(epfd);
close(listenfd);
return 0;
}
epoll机制
最新推荐文章于 2021-07-30 14:19:09 发布