Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Winsock non-blocking recv

This thread is locked; no one can reply to it. rss feed Print
Winsock non-blocking recv
Billybob
Member #3,136
January 2003

I want to be able to receive data from a socket if and only if it is available. If none is available I don't want it to wait for some, I want to continue with the rest of my program. I believe this is called non-blocking.

How would I go about doing that? It looks like select will tell be if there is data to read (or connections to accept, for listening sockets). But it has this timeout thing...I don't really want it to stop for any amount of time (beyond checking for data).

Any help?

Oscar Giner
Member #2,207
April 2002
avatar

Quote:

But it has this timeout thing...I don't really want it to stop for any amount of time

Pass 0 as timeout and it won't wait if there's no data ;)

Billybob
Member #3,136
January 2003

MSDN said:

Quote:

Set the timeout parameter to null for blocking operations.

?

Oscar Giner
Member #2,207
April 2002
avatar

I have this and it works (and it doesn't block)

1CServer::CServer()
2{
3 timeout.tv_sec = 0;
4 timeout.tv_usec = 0;
5}
6 
7bool CServer::IsConnectionWaiting()
8{
9 fd_set conn;
10 int s;
11 
12 FD_ZERO(&conn);
13 FD_SET(sock, &conn);
14 
15 s = select(0, &conn, NULL, NULL, &timeout);
16 
17 if (s>0) return true;
18 else return false;
19}

Notice that the timeout parameter is a pointer. What the docs say is that if you pass a NULL pointer the operation is blocking.

Billybob
Member #3,136
January 2003

You're the best Oscar ;)
I hope it works...

Go to: