Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


How do I check if there is data on the queue (Sockets)?

This topic is 6603 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im using sockets in my game, and I have a TCP socket and a UDP socket. Is there any way of checking to see if there is any incoming data before I call recv() or recvfrom(), which would both block. Or am I going to have to fire off another thread and block?

Share this post

Link to post
Share on other sites
You could use ioctl() to find out how much data you can read:
unsigned long size = 0;
int status = 0;
status = ioctl(sock, FIONREAD, &size); // Win32 uses ioctlsocket()
if (!status && size) do_whatever();

or use select() with a timeout of zero:
fd_set readfds;
struct timeval timeout;
FD_SET(sock, &readfds);
timeout.tv_sec = 0;
timeout.tv_usec = 0;
select(sock+1, &readfds, NULL, NULL, &timeout);
if (FD_ISSET(sock, &readfds)) do_whatever();

or set your sockets to non-blocking:
unsigned long val = 1;
ioctl(sock, FIONBIO, &val); // Win32 uses ioctlsocket

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
You could also use non-blocking sockets and just do the recv, testing to see if the socket would have blocked or not. Look at the FIONBIO option to ioctl (or ioctlsocket for Winsock)


Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!