Archived

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

stodge

Listen connection question

Recommended Posts

I have a listen connection that I use on my server to accept incoming connections from clients. The server can be configured to only accept a certain number of client connections, for example the server can only support five simultaneous connections. When the maximum number of support clients has been reached, I want to disable/close the listen connection, which I've done. However, if a client subsequently disconnects and frees one of these client "slots" then I want to re-enable/re-open the listen connection to allow another client to connect. I've coded this, but when I try to re-open the listen connection, I get an error:
quote:
EADDRINUSE - The specified address is already in use.
I specified that I wanted to re-use the address using setsockopts:
quote:
status = setsockopt(getSocket(), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); if (status == HB_INVALID) { hbNetEngine::getInstance()->setError(getLastError()); closeConnection(); return HB_FALSE; }
So I presumed that it would work, but it doesn't. Am I doing something fundamentally wrong? Or is there another way to do this? I thought about leaving the listen connection open, but then clients could still achieve a low level connection. I have the backlog set to 0. Thanks [edited by - stodge on November 5, 2002 10:15:20 PM]

Share this post


Link to post
Share on other sites
Off the top of my head the easiest approach to this problem would be to set the backlog to a very small number (say 1) on the initial call to listen and then just not call accept once you''re processing your limit of client connections.

As to your problems with your current implementation, it might help if there was more code to go off of, like how you are currently closing the listening socket, what the code looks like around your bind(), etc.

Share this post


Link to post
Share on other sites