Search code examples
c++socketsclient-serverportserver-side

Using different port numbers on server


I am pretty new to socket programming - so this might be a simple question but I'd really like to clarify.

I have a multiple-client to single server program: the individual clients send messages to the server which processes them, and then passes it on the destination i.e. the server is an intermediary.

On the server side, there is one thread for each client which is meant to 'listen' for messages from the clients (which will be placed in a buffer). At the moment all the clients are sending messages to the same port (as far as I can tell).

I am thinking of setting up another thread on which the server will process the messages before transmitting them on. Does it make sense to use another port on the server to send those messages?

I don't mean this to be a discussion, but I don't know what is common or more logical to do - any advice?

On the client-side, I am planning for it to have one thread for sending messages to the server, and another thread for receiving. Please let me know if any other information is required!

edit

At the moment it is a 1-server-to-multiple(tens now, hundreds later)-client program - I seem to have problems with the client receiving messages from my server (I am troubleshooting so I thought that using the same ports might be the problem), but I will try it with the same ports again and see. I thought it might be a matter of the receiving port being too busy to send messages as well.


Solution

  • At the moment all the clients are sending messages to the same port (as far as I can tell).

    What do you mean 'as far as I can tell'? You must know whether you are opening more than one port at the server.

    Does it make sense to use another port on the server to send those messages?

    No it doesn't. If you're using TCP, send the messages back down the same socket. If you're using UDP you don't need more than one UDP socket, and it simplifies the client and the application protocol if replies come from the same ip:port the request was sent from.