select应用于read函数 超时非阻塞方式
/*
* "Timed" read - timout specifies the # of seconds to wait before
* giving up (5th argument to select controls how long to wait for
* data to be readable). Returns # of bytes read or -1 on error.
*
* LOCKING: none.
*/
ssize_t
tread(int fd, void *buf, size_t nbytes, unsigned int timout)
{
int nfds;
fd_set readfds;
struct timeval tv; tv.tv_sec = timout;
tv.tv_usec = ;
FD_ZERO(&readfds);
FD_SET(fd, &readfds);
nfds = select(fd+, &readfds, NULL, NULL, &tv);
if (nfds <= ) {
if (nfds == )
errno = ETIME;
return(-);
}
return(read(fd, buf, nbytes));
} /*
* "Timed" read - timout specifies the number of seconds to wait
* per read call before giving up, but read exactly nbytes bytes.
* Returns number of bytes read or -1 on error.
*
* LOCKING: none.
*/
ssize_t
treadn(int fd, void *buf, size_t nbytes, unsigned int timout)
{
size_t nleft;
ssize_t nread; nleft = nbytes;
while (nleft > ) {
if ((nread = tread(fd, buf, nleft, timout)) < ) {
if (nleft == nbytes)
return(-); /* error, return -1 */
else
break; /* error, return amount read so far */
} else if (nread == ) {
break; /* EOF */
}
nleft -= nread;
buf += nread;
}
return(nbytes - nleft); /* return >= 0 */
}
select应用于read函数 超时非阻塞方式的更多相关文章
- socket设置为非阻塞方式(windows和linux)
Windows用以下方法将socket设置为非阻塞方式 : unsigned long ul=1; SOCKET s=socket(AF_INET,SOCK_STREAM,0); int ret=io ...
- fcntl()函数之非阻塞模型
优点:设置标准输入为非阻塞(有数据则读 没有数据则立即返回),常用于网络通信以及轻量信息多并发中 步骤: 1.oldflag=fcntl(STDIN_FILENO,F_GETFL); 获取标准输入的文 ...
- 用WINSOCK API实现同步非阻塞方式的网络通讯
Option Base 0Option Explicit '* ************************************************** *'* 模块名称:Winsock ...
- 非阻塞方式connect编程
参考博客: ①setsockopt()函数使用详解:http://blog.csdn.net/tody_guo/article/details/5972588 ②setsockopt :SO_LING ...
- linux C之getchar()非阻塞方式
参考链接: http://blog.csdn.net/zydlyq/article/details/50963360 #include "../include/CommUart.h" ...
- (转)非阻塞Connect对于select时应注意问题
对于面向连接的socket类型(SOCK_STREAM,SOCK_SEQPACKET)在读写数据之前必须建立连接,首先服务器端socket必须在一个客户端知道的地址进行监听,也就是创建socket之后 ...
- 阻塞、非阻塞的概念和select函数的阻塞功能
其它文档: http://www.cnitblog.com/zouzheng/archive/2010/11/25/71711.html (1)阻塞block 所谓阻塞方式block,顾名思义 ...
- socket阻塞与非阻塞,同步与异步,select,pool,epool
概念理解 一.与I/O相关的五个重要概念 1. 第一个概念:用户空间与内核空间 1. 现在操作系统都是采用虚拟存储器,那么对32位操作系统而言,它的寻址空间(虚拟存储空间)为4G(2的32次方) 2. ...
- 非阻塞式socket的select()用法
Select在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱用Select写程序,他们只 是习惯写诸如 connect.accept.recv或recvfrom这样的阻塞程 ...
随机推荐
- 关于Java中面向对象章节、IO 流中的重点基础知识。
一.面向对象的三大特征,以及作用. 答:面向对象的三大特征即,封装性.继承性.多态性. 其分别的作用为 : 封装作用:将数据封装起来,提高数据的安全性, 继承作用:提高代码的复用性,减少冗余代码. 多 ...
- iOS Notification – 远程通知
本文讲解iOS的远程通知的基本使用,主要包括远程通知的类型,处理远程通知的场景,以及远程通知相关证书的配置等等. 一.APNs简介 APNs是苹果公司提供的远程通知的服务器,当App处于后台或者没有运 ...
- Windows Server 2012 删除IIS之后 重新启动 桌面不出来 只出现一个命令提示框 解决方法
今天本来准备卸载 再重新安装一下IIS的,然后卸载的时候 可能是不小心 把 .net framework 给卸掉了 .net framework 带着powershell 所以卸掉之后 桌面快捷程序都 ...
- eclipse的xml文件提示templates的模板.md
eclipse的xml文件提示templates的模板 <?xml version="1.0" encoding="UTF-8" standalone=& ...
- SpringMVC过程中@RequestBody接收Json的问题 总是报415
在SpringMVC中用@RequestBody接收Json的问题,总是报415,经过一翻查找 前台js的post: var postdata = '{"title":" ...
- JavaScript对象遍历
一.对象是数组: var arr = [9, 8, 7, 6, 5, 4, 3, 2, 1]; //each遍历: $.each(arr,function (index,value) { alert( ...
- pandas数据分析(数据结构)
本文主要从以下两个方向对pandas的数据结构进行展开,分别为Series和DataFrame(对应的分别是系列与numpy中的一维数组和二维数组) 1.首先从Series讲起,主要介绍Series的 ...
- python基础教程(三)
序列概览 Python 包含6 种内建的序列,这里重点讨论最常用的两种类型:列表和元组. 列表与元组的主要区别在于,列表可以修改,元组则不能.也就是说如果要根据要求来添加元素,那么列表可以会更好用:而 ...
- tomcat设置文件编码
tomcat修改bin文件夹下面的catalina.bat文件可以解决乱码在文件中加上JAVA_OPTS="-server -Xms128M -Xmx4096M -XX:PermSize=5 ...
- django全文检索
-------------------linux下配置操作1.在虚拟环境中依次安装包 1.pip install django-haystack haystack:django的一个包,可以方便地对m ...