Sockets - pending data with recv(...)

Started by
3 comments, last by Squat 20 years, 8 months ago
Is there a way to check if there is any data awaiting to be received with recv(...) ? I don''t want my program to wait for data. I need to check if there is any data to receive as quick as possible. ================================= Ignorance is the root of all evil
=================================Ignorance is the root of all evil
Advertisement
nevermind, I found some code that can poll the sockets. I still have to figure out how it works lol

Here is what i found:

int status;
TIMEVAL timeVal;
timeVal.tv_sec = 0;
timeVal.tv_usec = 0;
fd_set readSet;
readSet.fd_count = 1;
readSet.fd_array[ 0 ] = mySocket;
status = select( mySocket + 1, &readSet, 0, 0, &timeVal );
if ( status > 0 )
{
recv....
}
=================================Ignorance is the root of all evil
Yes, what you found there will work. Looks like you might already have this but if not...

http://www.ecst.csuchico.edu/~beej/guide/net/html/

That''s pretty much the best sockets tutorial you will find on the net. Has a good section on non-blocking sockets with select which is what you are looking for. Keep in mind though that polling a socket in this manner will consume clock cycles so be sure to plan for that in your design or figure out another way of doing things. I don''t know what you are doing so i can''t offer any advice there.

Webby
just a proxy that can handle authentications as well in a secure manner. Thats why i need it to be fast. I don''t care too much for cpu usage - just connection speed
=================================Ignorance is the root of all evil
If its a graphical client you can look at WSAAsyncSelect(). select() isn''t too good on Windows for speed if you have lots of sockets to check.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis

This topic is closed to new replies.

Advertisement