views:

76

answers:

1

I'd like to open a socket and hang a readable event on the GLUT event loop... any ideas on how to do this? Portable standard GLUT code is best, but I'm open to platform-specific hacks as well.

Thanks!

+2  A: 

GLUT doesn't support this very well. See GLUT FAQ #18

You could register an idle function with glutIdleFunc, and in the idle function poll your socket to see if there's new data available. In order to avoid blocking when you read from your socket, you need to set your socket to be non-blocking by calling:

#include <unistd.h>
#include <fcntl.h>
...
sockfd = socket(PF_INET, SOCK_STREAM, 0);
fcntl(sockfd, F_SETFL, O_NONBLOCK);

(Taken from Beej's Guide to Networking)

The drawback of this approach is that your app will be checking the socket state 60 times a second, rather than just waiting for network data to come in.

You can also use the select(2) function to test if a socket has data available. See the man page at http://linux.die.net/man/2/select
Adam Rosenfield