void str_cli(FILE *fp, int sockfd)
{
int kq, i, n, nev, stdineof = , isfile;
char buf[MAXLINE];
struct kevent kev[];
struct timespec ts;
struct stat st; isfile = ((fstat(fileno(fp), &st) == ) &&
(st.st_mode & S_IFMT) == S_IFREG); EV_SET(&kev[], fileno(fp), EVFILT_READ, EV_ADD, , , NULL);
EV_SET(&kev[], sockfd, EVFILT_READ, EV_ADD, , , NULL); kq = kqueue();
ts.tv_sec = ts.tv_nsec = ;
kevent(kq, kev, , NULL, , &ts); for ( ; ; ) {
nev = kevent(kq, NULL, , kev, , NULL); for (i = ; i < nev; i++) {
if (kev[i].ident == sockfd) { /* socket is readable */
if ( (n = read(sockfd, buf, MAXLINE)) == ) {
if (stdineof == )
return; /* normal termination */
else
err_quit("str_cli: server terminated prematurely");
} write(fileno(stdout), buf, n);
} if (kev[i].ident == fileno(fp)) {   /* input is readable */
n = read(fileno(fp), buf, MAXLINE);
if (n > )
writen(sockfd, buf, n); if (n == || (isfile && n == kev[i].data)) {
stdineof = ;
shutdown(sockfd, SHUT_WR); /* send FIN */
kev[i].flags = EV_DELETE;
kevent(kq, &kev[i], , NULL, , &ts); /* remove kevent */
continue;
}
}
}
}
}

使用kqueue的str_cli函数的更多相关文章

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

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

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

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

  3. 使用fork的str_cli函数

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

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

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

  5. 使用/dev/poll的str_cli函数

    void str_cli(FILE *fp, int sockfd) { int stdineof; char buf[MAXLINE]; int n; int wfd; ]; struct dvpo ...

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

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

  7. 使用select的str_cli函数的实现

    void str_cli(FILE *fp, int sockfd) { int maxfdp1; fd_set rset; char sendline[MAXLINE], recvline[MAXL ...

  8. TCP回射客户程序:main函数

    创建套接字,装填网际网套接字地址结构 创建一个TCP套接字,用服务器的IP地址和端口号装填一个网际网套接字地址结构 我们可从命令行参数取得服务器的IP地址 从头文件unp.h取得服务器的众所周知端口号 ...

  9. 【unix网络编程第三版】阅读笔记(五):I/O复用:select和poll函数

    本博文主要针对UNP一书中的第六章内容来聊聊I/O复用技术以及其在网络编程中的实现 1. I/O复用技术 I/O多路复用是指内核一旦发现进程指定的一个或者多个I/O条件准备就绪,它就通知该进程.I/O ...

随机推荐

  1. SAP CRM Installed Bases(IBase)简介

    SAP CRM使用Installed Base(以下简称IBase)来组织服务相关对象并进行管理.因为我在最近的工作中经常接触这个概念,所以学习了一点相关文档.下面是文档的翻译. 本文链接:https ...

  2. 英语口语练习系列-C02-抱怨

    连接到英语口语系列总分类 连接到上一章棒棒的 竹石 郑燮 zhèng xiè 竹石 作者:郑燮 咬定青山不放松,立根原在破岩中. 千磨万击还坚劲,任尔东西南北风. Our team sucks. 我们 ...

  3. HTML5存储技术Storage

    前端存储技术localStorage是永久存储sessionStorage是一次会话存储 localStorage只支持string类型的存储 存进去的所有类型, 取出来之后都变成了string. 一 ...

  4. web框架开发-Django模型层(1)之ORM简介和单表操作

    ORM简介 不需要使用pymysql的硬编码方式,在py文件中写sql语句,提供更简便,更上层的接口,数据迁移方便(有转换的引擎,方便迁移到不同的数据库平台)…(很多优点),缺点,因为多了转换环节,效 ...

  5. odoo12.0 在Ubutu 18.04下环境的搭建

    sudo apt-get update sudo apt- postgresql nano virtualenv gcc python3.-dev libxml2-dev libxslt1-dev l ...

  6. 使用 xUnit 编写 ASP.NET Core 单元测试

    还记得 .NET Framework 的 ASP.NET WebForm 吗?那个年代如果要在 Web 层做单元测试简直就是灾难啊..NET Core 吸取教训,在设计上考虑到了可测试性,就连 ASP ...

  7. 乡下人设计模式——SOLID之六大原则

    S(Single Responsibility Principle):单一责任原则 O(Open Closed Principle):开放封闭原则 L(Liskov Substitution Prin ...

  8. vue中使用LESS、SASS、stylus

    less的使用 npm install less less-loader --save 修改webpack.config.js文件.vue.cli 搭建项目可跳过此步 { test: /\.less$ ...

  9. jQuery 合成事件

    jQuery有两个合成事件—— hover() 方法和 toggle() 方法,类似ready() 方法,hover() 方法和 toggle() 方法都属于 jQuery 自定义的方法. 1.  h ...

  10. 【apache】No input file specified

    默认的 RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]规则在apache fastcgi模式下会导致No input file specified. 修改成 Re ...