Non Blocking with recvfrom

Started by
4 comments, last by gimp 23 years, 11 months ago
I''v being pouring over MSDN for hours and can''t find any info on specifically HOW to create a ''recvfrom'' that just fails if there is no data in the buffer. Any pointers on this would be appreciated... thanks chris
Chris Brodie
Advertisement
When selecting on a socket with WSAAsyncSelect(), the socket should automatically be set to non-blocking operation.

If not using the asynchronous extensions ioctlsocket() should be used with the FIONBIO command to set non-blocking mode.

ex:
SOCKET some_socket = socket(blah, blah, blah);  // fill in blah''s with correct address family, type and protocollong arg = 1;int retVal = ioctlsocket(some_socket, FIONBIO, &arg); 
Thanks a lot. I''m wondering however if I am even coding this up correctly.

My server is a message router only. It collects packets from players, collates them, compresses them, then sends them out to all the other players.

My server app works by polling the socket. When it''s got a message it addes it to a queue. When 100ms has passed the loop enters the sending phase where it packages all the client requests and send them out to everyone with sendto.

Should I be using the event driven api''s in another thread? It would be more efficient on the cpu at least. I''d have to lock the list or take a copy when sending to ensure that new elements arn''t added to the queue array during sending though.

Do I have to worry about recieving multiple datagrams from multiple clients at the same time (UDP) or is there a queue behind recvfrom and I can just keep calling it until I get all the packets?

Thanks

Chris
Chris Brodie
Depending on the scale of your project I would probably use asynchronous event notification or blocking sockets in multiple threads instead of a polling method.

Also a single call of recvfrom() on a UDP packet will return at most a single datagram.
I''ve gone with threads, they were a lot easier to implement than I expected.. All I need to do now is read up on semaphores so I can ''lock'' arrays so I don''t start sending something that is only half written.

Since I have multiple threads I have modified the reader back to be blocking.

My only concern remaining from the last mail was that... what happens if more packets come in that I can read. Do that sit in a queue that recvfrom reads from or are they lost?

thanks
Chris Brodie
If more packets arrive than the incoming buffer can hold, packets will get dropped. Usually the buffer is something reasonable, so that as long as the rate you can consume the data is greater than rate that data arrives then there will usually not be a buffer overflow.

This topic is closed to new replies.

Advertisement