利用select和shutdown的str_cli函数
2015-05-04 12:52
162 查看
void str_cli(FILE *fp, int sockfd) { int maxfdp1, stdineof; fd_set rset; char buf[MAXLINE]; int n; stdineof = 0; FD_ZERO(&rset); for( ; ; ) { if(stdineof == 0) FD_SET(fileno(fp), &rset); FD_SET(sockfd, &rset); maxfdp1 = max(fileno(fp), sockfd) + 1; Select(maxfdp1, &rset, NULL, NULL, NULL); if(FD_ISSET(sockfd, &rset)) /* socket is readable */ { if((n = Read(sockfd, buf, MAXLINE)) == 0) { if(stdineof == 1) return; /* nomal termination*/ else err_quit("str_cli: server terminated "); } Write(fileno(stdout), buf, n); } if(FD_ISSET(fileno(fp), &rset)) /* input is readable */ { if((n = Read(fileno(fp), buf, MAXLINE)) == 0) { stdineof = 1; Shutdown(sockfd, SHUT_WR); /* send FIN */ FD_CLR(fileno(fp), &rset); continue; } Writen(sockfd, buf, n); } } }
str_cli函数,利用了select函数使客户端可以响应关闭服务器端连接,shutdown函数可以允许正确的批量输入。
相关文章推荐
- UNIX网络编程学习(14)--使用select正确处理EOF的str_cli函数修订版
- select使用实例——str_cli函数(修订版)
- UNIX网络编程学习(13)--使用select的str_cli函数的实现
- php中利用str_pad函数生成数字递增形式的产品编号
- 【廖雪峰 python教程 课后题 切片】利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法:
- linux—TCP_server端编写之利用select()函数编写可多用户同时访问
- Socket send函数和recv函数详解以及利用select()函数来进行指定时间的阻塞【转】
- Socket send函数和recv函数详解以及利用select()函数来进行指定时间的阻塞【转】
- Oracle select --字符函数:TRANSLATE(string,from_str,to_str)
- TCP回射客户程序:str_cli函数
- php中利用str_pad函数生成数字递增形式的产品编号
- 【廖雪峰 python教程 课后题 切片】利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法:
- php中利用str_pad函数生成数字递增形式的产品编号
- 利用Win32的网络函数创建一个网络浏览器
- select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- 利用backtrace和backtrace_symbols打印函数的调用关系
- ctypes 加载的so库中函数参数的字符串传递问题(str与bytes转换)
- Linux下select()函数的用法(转)
- 利用freopen()函数和fc命令简化程序调试
- shell脚本之 select 循环菜单及 函数