Hello everybody,
I've coded a multi-client server based on internet sockets using the scheme
listen on port X-accept-fork, exactly like beej's guide
At some point I would like to establish a secondary connection between a client and the server-child serving him.
I was considering the server-child bind() a socket to port X+1 listen() to it and then accept() a connect()ion by the client.But that can't be done, because if 2 server-children do that simultaneously, one would get "address already in use" error.
So then I've considered the client listen()ing on the Y+1 port (Y is the port assigned to the client's socket automatically by the primary connect() ).But my problem here is how can I find this port ? getpeername() on client's socket gives me the server's address & port.Is there any function giving the local address & port ?
I know that some problems remain. I am not guaranteed that port Y+1 is available.
A possible solution is that the client could send a free port number to the server through the primary connection and then the server-child connect()s to that port.
Or another one: The server-child knows the client's Y port when server-parent accepted() the connection.So server-child could send it back to the client.
But these aren't elegant solutions.Have you any ideas or suggestions?
Thank you