Jump to content
  • Advertisement
Sign in to follow this  
dpadam450

how to make recv() not block program

This topic is 4274 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

So in class we just briefly touched winsock. We used send() and recv(). My question is , how do I get revc() to not block my entire program?

Share this post


Link to post
Share on other sites
Advertisement
I know three way:

1. Use multithreading, one thread for network management and the other for the program.

2. If you use Windows, you can check MSDN for WSA function (WSAAccept(), WSAAsynchSelect(), WSAConnect(), WSARecv(), WSASend()) but I never really learned them.

3. Easiest way for you, check ioctlsocket()

unsigned long result = 0;
ioctlsocket( mySocket, FIONREAD, &result);
If result > 0 : Data waiting on buffer to do recv(), else continu with program.

Share this post


Link to post
Share on other sites
and here is a 4th way : the select() function
using the select you can put a timeout on the sockets
for recv or read data
syntax would be

struct timeval Timeout;
fd_set readfds;

readfds.fd_count = 1;
readfds.fd_array[0] = s;
Timeout.tv_sec = timeout;
Timeout.tv_usec = 0;

return(select(1, &readfds, NULL, NULL, &Timeout));

s is the socket , if timeout occurs then 0 will be returned and if data is waiting then the number of sockets waiting for operation will be returned.for more information check out the msdn.

if you are writing gui applications then the best option would be asynchronous sockets.read more at http://www.gamedev.net/reference/articles/article1297.asp

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!