C++ socket thread, concurrent server/client

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
I think you should try to create socket on stack, it allows threads to have own socket and I think prooblem will disappear
It sounds from your error message that your server may be trying to listen on the same port twice.

You should only have one place in your code where you listen for a connection on a given port. You should not be doing that on more than one thread.

Only after receiving the connection should you create a new thread to deal with that socket.

Maybe if you pasted some code it would help?
Last edited on
Reading through your post again it looks like your data server is being called to listen on the same port from multiple threads, one for each client request.

I think you need to make it so that you have two main threads. One for accepting the initial client request and one for accepting data server requests.

Then when you receive a client request maybe you could spin off a thread. In that thread you could then make a 'client socket request' to your data server socket, receiving a new client socket. Your thread could then simply act as a pipe between the data server newly acquired client socket and the original client socket.
Topic archived. No new replies allowed.