socket select 结果的说明

来源:互联网 发布:朴智妍网络剧重生 编辑:程序博客网 时间:2024/05/21 12:10

socket编程,select函数非常常用,例如:

int nEvents = select(socketHighestFd+1, &readFds, &writeFds, &exceptFds, &tv)

它的返回值nEvents,

如果readFds.fd_count = 1, writeFds.fd_count = 0, exceptFds.fd_count=0

表示只有一个端口在监听,比如一个web server,监听80端口,

此时client同时打开两个浏览器,输入它的网址进行访问。那么nEvents的值是多少?

 

答案:1

因为监听的这一个端口仅能处理一个请求。

所以在外加一个循环,第二次还能再得到一个结果:1

 

while(1) {

    nEvents = select(socketHighestFd+1, &readFds, &writeFds, &exceptFds, &tv)

}

 

我当时认为同时来了两个请求,结果就是2,这是错误的。。。

 

如果:readFds.fd_count = 2,其他为0,这两个端口有数据到来,此时结果就是:2

 

Return Values:

The select function returns the total number of socket handles that are ready and contained in the fd_set structures, zero if the time limit expired, or SOCKET_ERROR if an error occurred. If the return value is SOCKET_ERROR, WSAGetLastError can be used to retrieve a specific error code.

select函数返回已经准备并且包含在fd_set结构中socket进行处理的数目。

原创粉丝点击