Sockets – Is the port used by a TCP client to connect to a server unavailable for other TCP Servers

sockets

When creating a TCP client using the socket API, a port is used on the localhost for connecting to the TCP server.

The port that is used seems to be unavailable for another application which is a TCP server to bind to and act as a server.

Because the port used for the client is dynamically determined it may be a port that my application wants to use as a server.

Is it true that the TCP client will dynamically pick a port to use and prevent other programs from being a server on that port?

Can a client control what port it uses to make sure it does not occupy a port required by another program?

Best Answer

Is it true that the TCP client will dynamically pick a port to use and prevent other programs from being a server on that port?

Yes, It is.

Can a client control what port it uses to make sure it does not occupy a port required by another program?

Yes, you can but you should not. Use the Bind property.

OK .. heres the thing:

When you establish a connection to a server, you open a socket port that is greater than 1024. The point is, it is going to be a high port number.

Your server should not open a TCP port greater than 1024. Basically you should keep you server running in a low port. That is what all http documents tell us.

You can also check if a port are already taken, and if so, you can open your server socket in another port.

Related Topic