Advertisement

Non OS specific way of setting up a non-blocking socket

Started by April 11, 2002 08:36 AM
4 comments, last by Krylloan 22 years, 9 months ago
Is there a simple way of setting up a non-blocking socket in UDP that works under both Windows and Linux? Or, if not, one that doesn''t require knowledge about the hWnd etc. Or even better, a function like recvfrom() which doesn''t block? I''ve done a little bit of searching, but all I''ve seen is references to a WSA function that requires hWnd. Thanks Krylloan
Check out the select() function.
The function is part of the standard berkley sockets, and is available on almsot all platforms (afaik). WinSock contains a complete implementation of berkley sockets (with added extras)
Check Beej''s networking article on this site, especially the section at the end called "Slightly Advanced Topics" (i think) which shows how to use select() to create non-blocking sockets. Briefly, the select() function works by giving it a list of sockets, and it returns with the sockets that are waiting to be read/written to etc.
Advertisement
there is a function that you call on a socket which lets you modify whether the socket waits or not. unfortunatly i dont remember its name. it was something along ioctl, make sure you do searches for berkly socket fucntions.

EDIT: btw, once you set a socket to non blocking, you still use recvfrom(), sendto() and the other normal socket functions. though they wont block anymore. select() is useful since it lets you know when a socket is ready to be read/written to by having you pass it an array of sockets for it to check and a timeout value in which to return if no sockets are ready.

[edited by - a person on April 11, 2002 12:27:18 AM]
quote:
Original post by a person
there is a function that you call on a socket which lets you modify whether the socket waits or not. unfortunatly i dont remember its name. it was something along ioctl, make sure you do searches for berkly socket fucntions.



ioctl is correct for berkley, but in winsock you are supposed to call ioctlsocket. (Behavior is identical)

Here is an example of using it


  unsigned long EnableNonBlocking = 1;ioctlsocket(socket, FIONBIO, &EnableNonBlocking );  


Remember to check for the error EWOULDBLOCK (WSAEWOULDBLOCK), cause with asnychronous sockets you''ll be getting a lot of em! Also if your using TCP, your not guarenteed you''ll receive or send the entire packet or once, so you''ll have to piece them together using some sort of header id system.



- Kevin "BaShildy" King
Game Programmer: DigiPen
www.mpogd.com
- Kevin "BaShildy" KingGame Programmer: DigiPenwww.mpogd.com
I found it usful to wrap all these inconsistencies like close/closesocket and ioctl/ioctlsocket behind a small abstraction layer so that the calling code isn''t littered with #ifdefs and the like.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions ]
I agree. The key to cross-platform network compatibility is abstraction. Once you abstract away the details you can actually use the most efficient APIs available for each system.

Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com

This topic is closed to new replies.

Advertisement