概述
/*
*使用epoll实现的服务器
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <poll.h>
#include <sys/epoll.h>
#include <limits.h>
#include <errno.h>
#define MAXLINE 1024
#define OPEN_MAX 128
#define INFTIM -1
char buf[MAXLINE];
int main(int argc, char **argv)
{
int i, listenfd, connfd, sockfd, epollfd, nfds;
int nready;
ssize_t n;
socklen_t clilen;
struct pollfd client[OPEN_MAX];
struct sockaddr_in cliaddr, serveraddr;
struct epoll_event ev, events[20];
listenfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&serveraddr,sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(6610);
epollfd = epoll_create1(0);
ev.data.fd = listenfd;
ev.events = EPOLLIN|EPOLLET;
epoll_ctl(epollfd, EPOLL_CTL_ADD, listenfd, &ev);
bind(listenfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
listen(listenfd,100);
printf("ready for event loopn");
for(;;)
{
nfds = epoll_wait(epollfd, events, 20, -1);
for(i = 0;i < nfds; ++i)
{
if(events[i].data.fd == listenfd)
{
clilen = sizeof(cliaddr);
connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen);
if(connfd < 0)
{
perror("accept error");
continue;
}
printf("connection from %s, port is %dn", inet_ntop(AF_INET,&cliaddr.sin_addr,buf,sizeof(buf)),ntohs(cliaddr.sin_port));
ev.events = EPOLLIN|EPOLLET;
ev.data.fd = connfd;
if(epoll_ctl(epollfd, EPOLL_CTL_ADD, connfd, &ev) < 0)
{
fprintf(stderr, "epoll set insertion error: fd = %dn", connfd);
return -1;
}
}
else //operate socket
{
int n;
int sockfd = events[i].data.fd;
if(events[i].events & EPOLLIN) //fd is readable
{
bzero(buf, MAXLINE);
//printf("EPOLLIN readablen");
if(sockfd < 0)
continue;
if((n = read(sockfd, buf, MAXLINE)) < 0)
{
close(sockfd);
sockfd = -1;
perror("read error");
return ;
}
else if(n == 0)
{
printf("terminated from port %dn", ntohs(cliaddr.sin_port));
close(sockfd);
sockfd = -1;
}
else
{
printf("read %d charactersn", n - 1);
ev.data.fd = sockfd;
ev.events = EPOLLOUT|EPOLLET;
epoll_ctl(epollfd, EPOLL_CTL_MOD, sockfd, &ev);
}
}
if(events[i].events & EPOLLOUT) //fd is writeable
{
//printf("EPOLLOUT writeablen");
if(sockfd < 0)
continue;
if((n = write(sockfd, buf, strlen(buf))) < 0)
{
perror("write error");
return ;
}
else
{
printf("write %d charactersn", (n - 1) > 0?(n-1):0);
ev.data.fd = sockfd;
ev.events = EPOLLIN|EPOLLET;
bzero(buf, n);
epoll_ctl(epollfd, EPOLL_CTL_MOD, sockfd, &ev);
}
}
}
}
}
return 0;
}
打开3个终端,一个开启服务器,另外两个开启Telnet服务进行连接,模拟1个IP两个连接的情况。
最后
以上就是尊敬巨人为你收集整理的用epoll实现简单的服务器的全部内容,希望文章能够帮你解决用epoll实现简单的服务器所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复