Jump to content
  • Advertisement

Archived

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

DanTheRocker

WinSock Question

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

What is the proper way to have a host listen for incoming connections? Should it be like this: while( !bDone ) { while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) { if( msg.message == WM_QUIT ) { bDone = true; } else { TranslateMessage( &msg ); DispatchMessage( &msg ); } } NewSocket = accept( theSocket, NULL, NULL ); if( NewSocket != -1 ) { //

Share this post


Link to post
Share on other sites
Advertisement
No, you don''t want to do it that way. The accept() function is blocking. When you call accept(), it won''t return until a connection is actually received. So if nobody connects to your server, your application will just hang there and your message loop won''t get processed.

There are two ways around this:

First, you could make your sockets non-blocking and use asynchronous event notification. When you do this, WinSock sends you window messages that tell you its time to accept a new connection, receive data, send data, close a connection, etc.

Second, you could use multithreading. If your calls block in a different thread, it doesn''t matter, because your primary thread can still be processing messages and staying responsive to the user''s input.

Hope that helps a bit!

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.

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!