- 1
- 0
- 约8.2千字
- 约 8页
- 2016-09-21 发布于重庆
- 举报
epoll边缘触发(epollet)源代码例子
epoll边缘触发(epoll et) 源代码例子
/* ============================================================================ Name????????????????: epoll_test.c Author????????????: Version???????? : Copyright???? : Your copyright notice Description : epoll et example(echo) 此echo服务器对输入的内容复制了REPEAT_NUM(20000次),然后返回给客户端 ???????????????? 用于测试epollout事件如何触发。 ============================================================================ */ #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 #include sys/types.h #include sys/socket.h #include signal.h #define EPOLL_SIZE 10 #define EVENT_ARR 20 #define BACK_QUEUE 10 #define PORT 18001 #define BUF_SIZE 16 #define REPEAT_NUM 20000 #define OUT_BUF_SIZE 32*REPEAT_NUM int g_srv_fd; //由于有可能不能一次write所有的内容,所以需要全局变量保存内容的长度,内容输出到那里, //在监听到epollout事件后继续上一次的发送 char g_out_buf[OUT_BUF_SIZE];//保存输出的内容 int g_out_buf_offset;????????????????//保存输出到那里 int g_out_buf_len;???????????????????? //保存输出内容的长度 int g_has_write_buf;????????//保存是否要写输出内容 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); ??} } void handle_sig(int signum) { ??close(g_srv_fd); ??fprintf(stderr, receiv sig int); ??sleep(5); ??exit(0); } int write_out_buf(int fd, char *out_buf,int buf_len,int offset) { ??int snd_len = write(fd, out_buf+offset, buf_len-offset); ??int tmp_len; ??if (snd_len==(buf_len-offset)){ ????do{ ??????tmp_len = write(fd, out_buf+offset+snd_len, buf_len-offset-snd_len); ??????if (tmp_len0 tmp_len(buf_len-offset-snd_len)){ ????????snd_len += tmp_len; ????????break; ??????} ??????if(tmp_len == -1){ ????????break; ??????} ??????snd_len += tmp_len; ????}while(tmp_len0); ??} ??if (((snd_len==-1||tmp_len==-1
您可能关注的文档
最近下载
- 《管理体系审核指南gbt 19011-2021》详细解读.pptx VIP
- 四年级数学上册口算竖式计算脱式计算应用题每日一练(共30天).docx VIP
- 2026年永州职业技术学院高职单招职业适应性测试参考题库及答案解析.docx VIP
- 2023年永州职业技术学院高职单招数学历年高频考点试题含答案解析.docx VIP
- 2026年春学期青岛版小学数学一年级下册教学计划.docx VIP
- SHH--400SD药物稳定性试验箱.doc VIP
- 碳13呼气试验检测幽门螺杆菌课件.ppt VIP
- 【精选】数字电子技术课程设计指导书.pdf VIP
- 材料热力学动力学引言.ppt
- 税务机关稽查业务指南及案例分析.docx VIP
原创力文档

文档评论(0)