|
-
April 27th, 2009, 01:12 PM
#1
error 10022 on passing timeval to select
Hello,
I am trying to use this code with select:
Code:
struct timeval to;
to.tv_sec = 1;
to.tv_usec = 0;
select(static_cast<int>(maxfd+1), &listenSet, 0, 0, &to);
but when I run it gives me the error: 10022.. am I using the timeval struct wrong? I wanted it to pause for one sec and wait for an incoming connection.
Thanks!
-
April 27th, 2009, 08:41 PM
#2
Re: error 10022 on passing timeval to select
Actually, your usage of the struct timeval is correct. It is quite possible it is one of the other parameters is causing the error.
I normally do not perform the static_cast<int> as you have shown for the file descriptor set. This is how I typically use select():
Code:
fd_set savefds;
FD_ZERO(&savefds);
FD_SET(fd, &savefds);
while (true)
{
fd_set readfds = savefds; // this, and the next declaration need to be inside the loop
timeval to = {1, 0}; // to ensure they are initialized each time before select() is called
int sel = select(fd+1, &readfds, 0, 0, &to);
...
}
If you are monitoring more than one file descriptor, these of course are added individually to the file descriptor set. You also need to determine which is the largest file descriptor value, so you know the proper value to pass select() as the first parameter. For example:
Code:
int max_fd = std::max(fd1, fd2);
...
select(max_fd + 1, ...); // I'm not sure if the +1 is needed, but I always do it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|