Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

gimp

Non Blocking with recvfrom

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

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

Share this post


Link to post
Share on other sites
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 protocol
long arg = 1;
int retVal = ioctlsocket(some_socket, FIONBIO, &arg);

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!