Is the port used by the TCP client used to connect to the server inaccessible to other TCP servers

When creating a TCP client using the socket API, the port is used on the local host to connect to the TCP server.

The port used is not available for another application, which is a TCP server for binding and acting as a server.

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

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

Can the client control which port it uses to make sure that it does not occupy the port required by another program?

+7
source share
3 answers

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

Yes it is.

Can the client control which port it uses to make sure that it does not occupy the port required by another program?

Yes you can, but you shouldn't. Use the Bind property.

OK .. this thing:

When you establish a connection to the server, you open a socket port that is larger than 1024. The fact is that this will be a high port number.

The server should not open a TCP port greater than 1024. Basically, you should keep the server running in a low port. All http-documents say this.

You can also check if a port is already selected, and if so, you can open the server socket in another port.

+8
source

Yes, the port will be selected from a predefined range that varies from OS to OS and is blocked for other uses. You can select a specific port with bind if you need it.

EDIT:

The only case where you can have multiple TCP sockets connected to the same local port / IP is when you accept() new sockets from the listening socket. You can never bind() use a TCP port to use a port / IP. There is also some confusion in SO_REUSEADDR , this socket option does not allow port reuse, it just relaxes the rules when only dead timeouts are connected to the port you want.

+2
source

If one listening program is connected to a port, I don't think another can. Typically, the lower ports are reserved for certain services, such as HTTP, on port 80. If you need a random port, you must create one above 1024. For a list of commonly used ports, see here: http://en.wikipedia.org/wiki / List_of_TCP_and_UDP_port_numbers

edit: fixed from comment below

-one
source

All Articles