how to make recv() not block program

Started by
3 comments, last by PRASSHHANT 17 years, 6 months ago
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?

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Advertisement
ince you mention winsock I am assuming you are using Windows.


ioctlsocket


FIONBIO is the argument you use to change the blocking mode.

Edit: wrong url
"Pfft, Facts! Facts can be used to prove anything!" -- Homer J. Simpson
Clicky, ready. (Note: pretty much all of this stuff is the same whether you're using Unix or Windows.)
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.
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

This topic is closed to new replies.

Advertisement