epoll边缘触发(epollet)源代码例子.docVIP

  • 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

文档评论(0)

1亿VIP精品文档

相关文档