Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do TCP connections get moved to another port after they are opened? [duplicate]

If a TCP socket server listens on port 28081 for incoming connections and then accepts a connection and start receiving data. Is the port that data is coming into still 28081 or does the port get changed.

for example what port does the incoming data come to in the pseudo code below? Is it still 28081 or does the OS assign a new port?:

bind listen (on port 28081)  while 1   fd = accept   fork   if child process incoming data  
like image 721
Ivan Novick Avatar asked Sep 03 '10 19:09

Ivan Novick


People also ask

Are TCP connections reused?

The HTTP underlying tcp connection is not reused even though the requests are made to the same host.

Can you have multiple TCP connections on same port?

@FernandoGonzalezSanchez: A single client can have multiple TCP sockets bound to the same local IP/Port pair as long as they are connected to different remote IP/Port pairs.

Does TCP source port change?

There is no change to another port, like port 5000 in your example. FTP uses a control stream on port 21 and a data stream on a random high port, but HTTP does not, it stays on one port. There is one protocol that works the way you describe, TFTP over UDP, but no TCP based protocols, for the reasons in the answers.

How many TCP ports can be open at the same time?

You can have a total of 65,535 TCP Ports and another 65,535 UDP ports. When a program on your computer sends or receives data over the Internet it sends that data to an ip address and a specific port on the remote computer, and receives the data on a usually random port on its own computer.


2 Answers

A TCP connection is uniquely identified by two (IP address, TCP port) tuples (one for each endpoint). So by definition, one can't move a port or IP address of a connection but just open a different one.

If the server binds to port 28081 all accepted connections will have this port on the server side (although they most likely will have varying port numbers on the client side).

For example, if two processes from the same client machine will connect to the same server, the IP address and TCP port on the server side will be the same for both connections. On the client side however, they will have two different port numbers allowing the operating system on both sides to uniquely identify which process and file descriptor the received TCP packets should be assigned to.

like image 150
Andre Holzner Avatar answered Sep 28 '22 12:09

Andre Holzner


Yes, it stays on that port, though some protocols (FTP) might open a second connection on another port. Dont think of a port as a physical path or plug, like a USB port that can only have one thing plugged into it. But rather think of it as an identifier for the service being requested.

Often, though, the new socket connection is passed off to another thread which handles the read/writes for that specific connection.

like image 41
GrandmasterB Avatar answered Sep 28 '22 10:09

GrandmasterB