1. void str_cli(FILE *fp, int sockfd)
  2. {
  3. int stdineof;
  4. char buf[MAXLINE];
  5. int n;
  6. int wfd;
  7. struct pollfd pollfd[];
  8. struct dvpoll dopoll;
  9. int i;
  10. int result;
  11.  
  12. wfd = open("/dev/poll", O_RDWR, );
  13.  
  14. pollfd[].fd = fileno(fp);
  15. pollfd[].events = POLLIN;
  16. pollfd[].revents = ;
  17.  
  18. pollfd[].fd = sockfd;
  19. pollfd[].events = POLLIN;
  20. pollfd[].revents = ;
  21.  
  22. write(wfd, pollfd, sizeof(struct pollfd) * );
  23.  
  24. stdineof = ;
  25. for ( ; ; ) {
  26. /* block until /dev/poll says something is ready */
  27. dopoll.dp_timeout = -;
  28. dopoll.dp_nfds = ;
  29. dopoll.dp_fds = pollfd;
  30. result = ioctl(wfd, DP_POLL, &dopoll);
  31.  
  32. /* loop through ready file descriptors */
  33. for (i = ; i < result; i++) {
  34. if (dopoll.dp_fds[i].fd == sockfd) {
  35. /* socket is readable */
  36. if ( (n = read(sockfd, buf, MAXLINE)) == ) {
  37. if (stdineof == )
  38. return; /* normal termination */
  39. else
  40. err_quit("str_cli: server terminated prematurely");
  41. }
  42.  
  43. write(fileno(stdout), buf, n);
  44. } else {
  45. /* input is readable */
  46. if ( (n = read(fileno(fp), buf, MAXLINE)) == ) {
  47. stdineof = ;
  48. shutdown(sockfd, SHUT_WR); /* send FIN */
  49. continue;
  50. }
  51.  
  52. writen(sockfd, buf, n);
  53. }
  54. }
  55. }
  56. }

使用/dev/poll的str_cli函数的更多相关文章

  1. select使用实例——str_cli函数(修订版)

    我们可以使用select函数重写http://www.cnblogs.com/nufangrensheng/p/3587962.html中的str_cli函数,这样服务器进程一终止,客户就能马上得到通 ...

  2. 各I/O模型 对应Web服务应用模型(select,poll,epoll,kevent,"/dev/poll")

    一.利用select多路复用I/O的Web服务应用模型  /* 可读.可写.异常三种文件描述符集的申明和初始化.*/ fd_set readfds, writefds, exceptionfds; F ...

  3. TCP回射客户程序:str_cli函数

    str_cli函数完成客户处理循环: 从标准输入读入一行文本,写到服务器上,读回服务器对该行的回射,并把回射行写到标准输出上 读入一行,写到服务器 fgets读入一行文本,writen把该行发送给服务 ...

  4. /dev/poll, kqueue(2), event ports, POSIX select(2), Windows select(), poll(2), and epoll(4)

    /dev/poll, kqueue(2), event ports, POSIX select(2), Windows select(), poll(2), and epoll(4) libevent ...

  5. 详解网络编程必会的poll和epoll函数

    前言 之前已经介绍过select函数,请参考这篇博客:https://www.cnblogs.com/liudw-0215/p/9661583.html,原理都是类似的,有时间先阅读下那篇博客,以便于 ...

  6. 使用fork的str_cli函数

    void str_cli(FILE *fp, int sockfd) { pid_t pid; char sendline[MAXLINE], recvline[MAXLINE]; ) { /* ch ...

  7. 非阻塞读和写:str_cli函数

    void str_cli(FILE *fp, int sockfd) { int maxfdp1, val, stdineof; ssize_t n, nwritten; fd_set rset, w ...

  8. 使用kqueue的str_cli函数

    void str_cli(FILE *fp, int sockfd) { , isfile; char buf[MAXLINE]; ]; struct timespec ts; struct stat ...

  9. 使用select正确处理EOF的str_cli函数

    void str_cli(FILE *fp, int sockfd) { int maxfdp1, stdineof; fd_set rset; char buf[MAXLINE]; int n; s ...

随机推荐

  1. 【English】20190321

    Keep in mind记住[kip ɪn maɪnd]  maintenance维护[ˈmentənəns] Also Keep in mind that table maintenance use ...

  2. R语言学习——矩阵

    > #矩阵是一个二维数组,每个元素都拥有相同的模式(数值型.字符型或者逻辑型).通过matrix()创建,一般使用格式为:mymatrix<-matrix(vector,nrow=numb ...

  3. 使用jprofiler分析dump文件一个实例

    3 .dump 线上文件栈 [root@yszyz10a153 ~]# jmap -dump:live,format=b,file=heap201712.hropf  72947 参考:https:/ ...

  4. Linux内存管理 (22)内存检测技术(slub_debug/kmemleak/kasan)

    专题:Linux内存管理专题 关键词:slub_debug.kmemleak.kasan.oob.Redzone.Padding. Linux常见的内存访问错误有: 越界访问(out of bound ...

  5. HNOI2019做题笔记

    代码比较长所以直接去LOJ看吧- 鱼(计算几何.向量) 比较套路的内容:枚举\(D\),对于其他所有点按照\(D\)极角排序,按照极角序枚举\(A\),这样垂直于\(AD\)的线也会以极角序旋转,可以 ...

  6. 调参必备---GridSearch网格搜索

    什么是Grid Search 网格搜索? Grid Search:一种调参手段:穷举搜索:在所有候选的参数选择中,通过循环遍历,尝试每一种可能性,表现最好的参数就是最终的结果.其原理就像是在数组里找最 ...

  7. cmd执行超大sql文件

    osql -S 127.0.0.1 -U sa -P 123456 -i d:\test.sql osql为SQL Server的命令,要在cmd中执行该命令,一般安装完SQL Server后该命令对 ...

  8. DAY11、函数总结

    一.函数的对象 1.函数对象:函数名存放的就是函数的地址,所以函数名也是对像 2.函数对象的应用: 2.1.可以直接被引用   fn = cp_fn 2.2.可以当作函数参数传递    compute ...

  9. AngularJS 1.x系列:AngularJS服务-Service、Factory、Provider、Value及Constant(5)

    1. AngularJS服务 AngularJS可注入类型包括:Service.Factory.Provider.Value及Constant. 2. Service AngularJS Servic ...

  10. Java垃圾收集器概述

    垃圾收集器的操作 查找未使用的对象,释放内存,并压缩堆,避免内存碎片 一个java程序,有执行应用程序逻辑的线程和执行GC的线程组.当GC跟踪对象引用,或在内存中移动对象,它必须确保应用程序线程没有使 ...