• Advertisement
Sign in to follow this  

C++ & Winsock: waiting for connections blocks my program

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

Hello, Im making a simple chatting program. I managed to get non-blocking sockets to work, but the problem is that the program hangs when the server waits for incoming connections, until one client gets connected (one user makes a server and then others connect to it and chat). I call listen and then accept. The program hangs at the accept function until it gets a connection from one client. How would I get skip this accept call if there are no connections (and check for new connections every second or so)?

Share this post


Link to post
Share on other sites
Advertisement
assuming C:

MSDN quote: The accept function can block the caller until a connection is present if no pending connections are present on the queue, and the socket is marked as blocking. If the socket is marked as nonblocking and no pending connections are present on the queue, accept returns an error as described in the following. After the successful completion of accept returns a new socket handle, the accepted socket cannot be used to accept more connections. The original socket remains open and listens for new connection requests.

so, check carefully if the socket is created as a non blocking one.

Share this post


Link to post
Share on other sites
You can also use multi threading to do different tasks or use the select function (sockets) to check if there is a message and if there is, accept connection.

Share this post


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

  • Advertisement