开发者

select() behaviour for writeability?

I have a fd_set "write_set" which contains sockets that I want to use in a send(...) call. When I call select(maxsockfd+1, NULL, &write_set, NULL, &tv) there it always returns 0 (timeout) although I haven't sent anything over the sockets in the write_set yet and it should be possible to send data.

Why is this? Shouldn't select return instantly when it's possible to send data over the sockets in write_set?

Thanks!

Edit: My code..

// _read_set and _write_set are the master sets
fd_set read_set = _read_set;
fd_set write_set = _write_set;

// added this for testing, the socket is a member of RemoteChannelConnector.
std::list<RemoteChannelConnector*>::iterator iter;
for (iter = _acceptingConnectorList->begin(); iter != _acceptingConnectorList->end(); iter++) {

    if(FD_ISSET((*iter)->getSocket(), &write_set)) {

        char* buf = "a";
        int ret;
        if ((ret = send((*iter)->getSocket(), buf, 1, NULL)) == -1) {
            std::cout << "error." << std::endl;
        } else {
            std::cout << "success." << std::endl;
        }

    }

}

struct timeval tv;
tv.tv_sec = 10;
tv.tv_usec = 0;

int status;

if ((status = select(_maxsockfd, &read_set, &write_set, NULL, &tv)) == -1) {

    // Terminate process on error.
    exit(1);

} else if (status == 0开发者_开发知识库) {

    // Terminate process on timeout.
    exit(1);

} else {
// call send/receive
}

When I run it with the code for testing if my socket is actually in the write_set and if it is possible to send data over the socket, I get a "success"...


I don't believe that you're allowed to copy-construct fd_set objects. The only guaranteed way is to completely rebuild the set using FD_SET before each call to select. Also, you're writing to the list of sockets to be selected on, before ever calling select. That doesn't make sense.

Can you use poll instead? It's a much friendlier API.


Your code is very confused. First, you don't seem to be setting any of the bits in the fd_set. Secondly, you test the bits before you even call select.

Here is how the flow generally works...

  • Use FD_ZERO to zero out your set.
  • Go through, and for each file descriptor you're interested in the writeable state of, use FD_SET to set it.
  • Call select, passing it the address of the fd_set you've been calling the FD_SET function on for the write set and observe the return value.
  • If the return value is > 0, then go through the write set and use FD_ISSET to figure out which ones are still set. Those are the ones that are writeable.

Your code does not at all appear to be following this pattern. Also, the important task of setting up the master set isn't being shown.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜