Jump to content
  • Advertisement
Sign in to follow this  
KingdomHeart

OpenGL and Socket

This topic is 3940 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 wrote a 2 versions of a battleship game. I wrote a text-base game with socket code. So client and server talk to each other though the socket. So the way I lay out my code is that, I constantly check the socket to see if anything come in, if there is, then I record it as an attack, then I attack back. Now I try to do the same thing but in graphics-base with openGL. I stumble on deciding where to put the piece the client code. I cant put it in main() because, it will hang on to the while loop that I check to see if anything come into the socket. Therefore It will never get to glutMainLoop(), which process all my graphics event. I guess my question is that, is there a callback function that constantly check the socket, if not, how do I implement socket in this situation. Thank you.

Share this post


Link to post
Share on other sites
Advertisement
Its simply a case of not hanging on the socket listen. Look up the function select(). This examines a set of 1 or more sockets and tells you their status for reading and writing.

Also, a more complicated (and in this case rather unnecessary) alternative solution is to put the opengl renderer in its own thread.

Share this post


Link to post
Share on other sites
Can you elaborate more on how to use select() to detect things that coming into the socket. Because I use select(), but not to detect things from socket, but to see if any new client show up. So I use select() in my server code. The server detect clients showing up, and pair up 2 clients. when client attack, the server just pass the attack from 1 client to another, so the client wait to see if any thing come in from the socket. That is how I code it. I think it more like a client base game rather than a server base game.

Share this post


Link to post
Share on other sites
select() just tells you if there is any activity on the sockets you supply it with. You only need to read from sockets where select() has told you there is information to be read. Just add all relevant sockets to the FD_SET, pass that in as the readfds parameter of select, and examine the output to see which sockets need reading from.

Share this post


Link to post
Share on other sites
If your client application is running a frequent loop anyway, I believe that polling the (nonblocking) socket is just as good an approach as any.

So just call a socket polling function inside your main loop, which reads as much data as there is on the socket, breaks it up / recombines it into distinct messages and processes as many (complete) messages as it has through your message processing function.

This isn't really terribly difficult, certainly much more straightforward than faffing around with select() etc (although that's not hard either).

Likewise, keeping your game entirely single threaded will make it easier to write (albeit may not use multiple CPUs / cores efficiently).

If you're using a fixed-timestep-with-interpolation system for rendering, then you probably want to do network polling inside your timestep processing function.

Mark

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!