#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#include <errno.h>
#define EPOLL_SIZE 10
#define EVENT_ARR 20
#define BACK_QUEUE 10
#define PORT 18001
#define BUF_SIZE 16
void
setnonblocking(
int
sockFd) {
int
opt;
opt = fcntl(sockFd, F_GETFL);
if
(opt < 0) {
printf
(
"fcntl(F_GETFL) fail."
);
exit
(-1);
}
opt |= O_NONBLOCK;
if
(fcntl(sockFd, F_SETFL, opt) < 0) {
printf
(
"fcntl(F_SETFL) fail."
);
exit
(-1);
}
}
int
main() {
int
serverFd;
serverFd = socket(AF_INET, SOCK_STREAM, 0);
setnonblocking(serverFd);
int
epFd = epoll_create(EPOLL_SIZE);
struct
epoll_event ev, evs[EVENT_ARR];
ev.data.fd = serverFd;
ev.events = EPOLLIN | EPOLLET;
epoll_ctl(epFd, EPOLL_CTL_ADD, serverFd, &ev);
struct
sockaddr_in serverAddr;
socklen_t serverLen =
sizeof
(
struct
sockaddr_in);
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddr.sin_port = htons(PORT);
if
(bind(serverFd, (
struct
sockaddr *) &serverAddr, serverLen)) {
printf
(
"bind() fail./n"
);
exit
(-1);
}
if
(listen(serverFd, BACK_QUEUE)) {
printf
(
"Listen fail./n"
);
exit
(-1);
}
int
clientFd;
sockaddr_in clientAddr;
socklen_t clientLen;
char
buf[BUF_SIZE];
while
(1) {
int
nfds = epoll_wait(epFd, evs, EVENT_ARR, -1);
for
(
int
i = 0; i < nfds; i++) {
if
(evs[i].data.fd == serverFd && evs[i].data.fd & EPOLLIN) {
if
((clientFd = accept(serverFd,
(
struct
sockaddr *) &clientAddr, &clientLen)) < 0) {
printf
(
"accept fail./n"
);
}
printf
(
"Connect from %s:%d/n"
, inet_ntoa(clientAddr.sin_addr),
htons(clientAddr.sin_port));
setnonblocking(clientFd);
ev.data.fd = clientFd;
ev.events = EPOLLIN | EPOLLET;
epoll_ctl(epFd, EPOLL_CTL_ADD, clientFd, &ev);
}
else
if
(evs[i].events & EPOLLIN) {
if
((clientFd = evs[i].data.fd) > 0) {
int
len = read(clientFd, buf, BUF_SIZE);
if
(len > 0) {
do
{
if
(write(clientFd, buf, len) < 0) {
printf
(
"write() fail./n"
);
}
len = read(clientFd, buf, BUF_SIZE);
}
while
(len > 0);
}
else
if
(len == 0) {
printf
(
"Client closed at %d/n"
, clientFd);
epoll_ctl(epFd, EPOLL_CTL_DEL, clientFd, &ev);
close(clientFd);
evs[i].data.fd = -1;
break
;
}
else
if
(len == EAGAIN) {
continue
;
}
else
{
printf
(
"read() fail."
);
}
}
}
else
{
printf
(
"other event./n"
);
}
}
}
return
0;
}
相关推荐
myserver是一个基于epoll模型的异步通信、返回结果为JSON的高性能服务器。它使用C 编写,需要通过mylog以及myconf这二者做编译依赖。和外界交互为JSON格式(使用开源库rapidjson)。用户只需要配置好服务器,开始...
在Linux 系统下面用C 语言实现的高并发服务器的代码,具体用到了Epoll,线程池,数据库连接池。 具体可以看下连接http://blog.csdn.net/wuyuxing24/article/details/48758927
epoll是Linux内核为处理大批量文件描述符而作了改进的epoll,是Linux下多路复用IO接口select/poll的增强版本,它能显著提高程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率。
FIFO配合管道PIPE,实现FIFO传递数据,PIPE触发epoll,异步IO效果
linux 通过 epoll 实现 tcp 服务器 代码
epoll高效处理消息,通过epoll的level出发模式实现
基于Tcp、epoll实现http1.1服务器 支持php参数传递及html,该程序可以直接放到虚拟机编译运行,测试结合浏览器进行访问
(无需积分,免费!!!)fd_event_list = epl.poll() # 进行循环解包,进一步判断 for fd, event in fd_event_list: # 循环第一次,如果 文件描述符 是tcp_socket的,就进行侦听分离new_socket ...
Linux下基于epoll 线程池高并发服务器实现研究.pdf
linux 下的 用 epoll 开发的 并发服务器 模型
https://blog.csdn.net/u014220105/article/details/84849570 可先参考相关描述,epoll实现tcp百万级高并发测试,代码仅用于测试和功能验证,并不是特别完善,不喜勿喷,欢迎指导
一个非常好的epoll+线程池服务器Demo
linux下完整的epoll多线程高并发服务器代码
linux下的epoll版telnet服务器。。linux下的epoll版telnet服务器。。linux下的epoll版telnet服务器。。linux下的epoll版telnet服务器。。linux下的epoll版telnet服务器。。linux下的epoll版telnet服务器。。linux下...
linux epoll 服务器,windows socket客户端
EPOLL多线程TCP服务器,可以动态获取连接客户机并且向客户机发送数据
服务器与客户端建立连接需要使用到一些接口,包括但不限于socket、bind、listen、accept.高并发编程会有一些服务器模型,例如reactor或proactor。这两类都要使用到IO多路复用,O多路复用是指单个进程/线程就可以同时...
在linux下通过epoll实现udp服务器的同时收发工作
使用Python做的epoll高效率服务器。使用平台:Linux,windows不支持
在linux系统下用epoll实现的简易聊天,简单的qq。所有源码都在里面,直接可运行,分为客户端和服务器两个文件夹