Click here to Skip to main content
15,064,166 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)

Hi all;
Im writing a concurrent server that's supposed to have a communication channel and a data channel.

The client initially connect to the communication channel to authenticate, upon successful authentication, the client is then connected to the data channel to access data.

My program is already doing that, and im using threads.My only issue is that if I try to connect another client, I get a "cannot bind : address already in use" error.

I have it this way:

PART A
---------
Client connect to port 4567 (and enter his login info). A thread spawn to handle the client(repeated for each client that connects). In the thread created, I have a function(let's call it FUNC_A) that checks the client's login info(dont worry about how the check is done), if successful, the thread starts the data server(listening on 8976) then sends an OK to the client, once received the client attempts to connect to the data server.

PART B
--------
Once a client connect to the data server, from inside FUNC_A the client is accepted and another thread is spawn to handle the client's connection to the data server.(hopefully everything is clear).
Now, all that is working fine. However, if I try to connect with second client when it gets to PART B I get a "cannot bind error: address already in use". I've tried so many different ways, I've even tried spawning a thread to start the data server and accept the client and then start another thread to handle that connection. still no luck.

Please give me a suggestion as to what I'm doing wrong, how do I go about doing this or what's the best way to implement it.
Thank you
Posted
Updated 12-May-10 1:11am
v2

You cannot bind a socket to an ip address and port more than once.
Check if your bind call is doing this.
I'm guessing you're calling bind inside a loop.
If so, try moving it out of the loop.

I would also recommend you use I/O Completion ports.
   
pigre wrote:
if I try to connect another client, I get a "cannot bind : address already in use" error.


Only one socket can bind/listen on a given port number at any time, you can not start "a second server" on the same port number. If your technical requirements absolutely need two separate servers (one on port 4567 and one on 8976), then start them together at application start-up.

In other words bind the socket only once on the server side: socket, bind, listen and then per incoming client accept.

Hope this helps :)
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900