开发者

Unix Sockets: select() with more than one set associated does more than it should do

I have the following select call for tcp sockets:

ret = select(nfds + 1, &rfds, &rfds开发者_如何学JAVA2, NULL, &tv);

rfds2 is used when I send to large data (non-blocking mode). And rfds is there to detect if we received something on the socket.

Now, when the send buffer is empty, I detect it with rfds2. But at the same time I get the socket back in rfds, although there is nothing that I received on that socket.

Is that the intended behaviour of the select-call? How can I distinguish orderly between the send and the recieve case?


Now, when the send buffer is empty, I detect it with rfds2

That's not correct. select() will detect when the send buffer has room. It is hardly ever correct to register a socket for OP_READ and OP_WRITE simultaneously. OP_WRITE is almost always ready, except in the brief intervals when the send buffer is full.


Thanks for your answers. I have found the problem for myself: The faulty code was after the select call (how I used FD_ISSET() to determine which action I can do).

I think my assumption is true, that there is only a socket in rfds, when there is really some data that can be received.


If the socket is non-blocking that seems to be the expected behaviour. The manual page for select has this to say about the readfds argument:

Those listed in readfds will be watched to see if characters become available for reading (more precisely, to see if a read will not block; in particular, a file descriptor is also ready on end-of-file)

Because the socket is non-blocking it is true that a read would not block and hence it is reasonable for that bit to be set.

It shouldn't cause a problem because if you try and read from the socket you will simply get nothing returned and the read won't block.

As a rule of thumb, whenever select returns you should process each socket that it indicates is ready, either reading and processing whatever data is available if it returns as ready-to-read, or writing more data if it returns as ready-to-write. You shouldn't assume that only one event will be signalled each time it returns.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜