This project has moved and is read-only. For the latest updates, please go here.

Question about port numbers

Mar 29, 2012 at 11:29 AM

Hi, I've written a simple client/server app based on the sample code in the Nito download. I'm using port 3000 and the server displays a message confirming that it is listening on this port. However when it accepts a client connection it shows a message similar to "Connection established to", and this port number increments each time a client connects - I'm really just curious as to what this port number is and how it relates to the port that the server is listening on and that the clients connect to (i.e. 3000).

Thanks in advance


Apr 25, 2012 at 1:49 AM

(Sorry for the late reply!)

TCP ports are a bit odd, but they make sense once you get used to them.

Let's start with the server side. A server opens a "listening" socket on a specific "bound" port (e.g., 3000). Whenever a new client connects, the server actually grabs an unused port number and uses that for the connection - so the server-side port that the client actually communicates with is some port other than 3000.

A similar thing happens on the client side, except the client doesn't "listen" - or "bind" (usually). So the client just grabs an unused port number and uses that to connect to the server. The client connects to port 3000, but then it communicates to another port.

So when the server says "Connection established to", it's saying that the client IP is (i.e., the local machine) and the client's port is 49641. There's no relation to the server port number at all.

You may find these blog posts to be helpful: