how to make recv() not block program
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?
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
ioctlsocket
FIONBIO is the argument you use to change the blocking mode.
Edit: wrong url
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.
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
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
Popular Topics
Advertisement