select函数中第三个参数:writefds,大家是怎么理解和应用?

VampireQQ 2002-10-12 06:39:38
在常用的服务器/客户机的模型中,服务器端使用select,客户端连接到服务器,服务器就分配一个socket fd,所有的数据读写都通过这个socket fd,一旦客户端使用send等函数发送消息,服务器端的fd_isset就会成立接下来就是服务器端的处理消息的函数了,但是服务器端要往客户端发送信息的话,我会使用send等函数,这个时候我怎么样监测writefds呢?请大家指点。
...全文
747 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
everwindforce 2002-11-03
  • 打赏
  • 举报
回复
The select Model
The select model is the most widely available I/O model in Winsock. We call it the select model because it centers on using the select function to manage I/O. The design of this model originated on Unix-based computers featuring Berkeley socket implementations. The select model was incorporated into Winsock 1.1 to allow applications that want to avoid blocking on socket calls the ability to manage multiple sockets in an organized manner. Because Winsock 1.1 is backward-compatible with Berkeley socket implementations, a Berkeley socket application that uses the select function should technically be able to run without modification.

The select function can be used to determine whether there is data on a socket and whether a socket can be written to. The whole reason for having this function is to prevent your application from blocking on an I/O bound call such as send or recv when a socket is in a blocking mode and to prevent the WSAEWOULDBLOCK error when a socket is in nonblocking mode. The select function blocks for I/O operations until the conditions specified as parameters are met. The function prototype for select is as follows:

int select(
int nfds,
fd_set FAR * readfds,
fd_set FAR * writefds,
fd_set FAR * exceptfds,
const struct timeval FAR * timeout
);




The first parameter, nfds, is ignored and is included only for compatibility with Berkeley socket applications. You'll notice that there are three fd_set parameters: one for checking readability (readfds), one for writability (writefds), and one for out-of-band data (exceptfds). Essentially, the fd_set data type represents a collection of sockets. The readfds set identifies sockets that meet one of the following conditions:


Data is available for reading.


Connection has been closed, reset, or terminated.


If listen has been called and a connection is pending, the accept function will succeed.

The writefds set identifies sockets in which one of the following is true:


Data can be sent.


If a nonblocking connect call is being processed, the connection has succeeded.

Finally, the exceptfds set identifies sockets in which one of the following is true:


If a nonblocking connect call is being processed, the connection attempt failed.


Out-of-band (OOB) data is available for reading.

maoyujian 2002-11-01
  • 打赏
  • 举报
回复
监测writefds是这个套接字可写是返回还是正在发送数据是返回?
gongdath 2002-10-13
  • 打赏
  • 举报
回复
select函数在writefds变为可写时返回.
VampireQQ 2002-10-13
  • 打赏
  • 举报
回复
up
whydoyoucare 2002-10-13
  • 打赏
  • 举报
回复
《windows网络编程技术》第八章内容:
只有在三种条件下,才会发出F D _ W R I T E通知:
■ 使用c o n n e c t或W S A C o n n e c t,一个套接字首次建立了连接。
■ 使用a c c e p t或W S A A c c e p t,套接字被接受以后。
■ 若s e n d、W S A S e n d、s e n d t o或W S A S e n d To操作失败,返回了W S A E W O U L D B L O C K错
误,而且缓冲区的空间变得可用
因此,作为一个应用程序,自收到首条F D _ W R I T E消息开始,便应认为自己必然能在一个套接字上发出数据,直至一个s e n d、W S A S e n d、s e n d t o或W S A S e n d To返回套接字错误
W S A E W O U L D B L O C K。经过了这样的失败以后,要再用另一条F D _ W R I T E通知应用程序再次发送数据。
VampireQQ 2002-10-12
  • 打赏
  • 举报
回复
其实就是究竟在什么时候,场合使用writefds?
Select在Socket编程还是比较重要的,可是对于初学Socket的人来说都不太爱用Select写程序,他们只是习惯写诸如connect、accept、recv或recvfrom这样的阻塞程序(所谓阻塞方式block,顾名思义,就是进程或是线程执行到这些函数时必须等待某个事件的发生,如果事件没有发生,进程或线程就被阻塞,函数不能立即返回)。可是使用Select就可以完成非阻塞(所谓非阻塞方式non-block,就是进程或线程执行此函数时不必非要等待事件的发生,一旦执行肯定返回,以返回值的不同来反映函数的执行情况,如果事件发生则与阻塞方式相同,若事件没有发生则返回一个代码来告知事件未发生,而进程或线程继续执行,所以效率较高)方式工作的程序,它能够监视我们需要监视的文件描述符的变化情况——读写或是异常。下面详细介绍一下! Select函数格式(我所说的是Unix系统下的伯克利socket编程,和windows下的有区别,一会儿说明): int select(int maxfdp,fd_set *readfds,fd_set *writefds,fd_set *errorfds,struct timeval *timeout); 先说明两个结构体: 第一,struct fd_set可以理解为一个集合,这个集合存放的是文件描述符(file descriptor),即文件句柄,这可以是我们所说的普通意义的文件,当然Unix下任何设备、管道、FIFO等都是文件形式,全部包括在内,所以毫无疑问一个socket就是一个文件,socket句柄就是一个文件描述符。fd_set集合可以通过一些宏由人为来操作,比如清空集合FD_ZERO(fd_set *),将一个给定的文件描述符加入集合之FD_SET(int ,fd_set *),将一个给定的文件描述符从集合删除FD_CLR(int ,fd_set*),检查集合指定的文件描述符是否可以读写FD_ISSET(int ,fd_set* )。一会儿举例说明。 第二,struct timeval是一个大家常用的结构,用来代表时间值,有两个成员,一个是秒数,另一个是毫秒数。 具体解释select参数: int maxfdp是一个整数值,是指集合所有文件描述符的范围,即所有文件描述符的最大值加1,不能错!在Windows这个参数的值无所谓,可以设置不正确。 fd_set *readfds是指向fd_set结构的指针,这个集合应该包括文件描述符,我们是要监视这些文件描述符的读变化的,即我们关心是否可以从这些文件读取数据了,如果这个集合有一个文件可读,select就会返回一个大于0的值,表示有文件可读,如果没有可读的文件,则根据timeout参数再判断是否超时,若超出timeout的时间,select返回0,若发生错误返回负值。可以传入NULL值,表示不关心任何文件的读变化。 fd_set *writefds是指向fd_set结构的指针,这个集合应该包括文件描述符,我们是要监视这些文件描述符的写变化的,即我们关心是否可以向这些文件写入数据了,如果这个集合有一个文件可写,select就会返回一个大于0的值,表示有文件可写,如果没有可写的文件,则根据timeout参数再判断是否超时,若超出timeout的时间,select返回0,若发生错误返回负值。可以传入NULL值,表示不关心任何文件的写变化。 fd_set *errorfds同上面两个参数的意图,用来监视文件错误异常。 struct timeval* timeout是select的超时时间,这个参数至关重要,它可以使select处于三种状态,第一,若将NULL以形参传入,即不传入时间结构,就是将select置于阻塞状态,一定等到监视文件描述符集合某个文件描述符发生变化为止;第二,若将时间值设为0秒0毫秒,就变成一个纯粹的非阻塞函数,不管文件描述符是否有变化,都立刻返回继续执行,文件无变化返回0,有变化返回一个正值;第三,timeout的值大于0,这就是等待的超时时间,即select在timeout时间内阻塞,超时时间之内有事件到来就返回了,否则在超时后不管怎样一定返回,返回值同上述。 返回值: 负值:select错误 正值:某些文件可读写或出错 0:等待超时,没有可读写或错误的文件

4,359

社区成员

发帖
与我相关
我的任务
社区描述
通信技术相关讨论
社区管理员
  • 网络通信
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧