Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a system's TCP/IP stack differentiate between multiple programs connecting to the same address and port?

Tags:

tcp

Suppose two web browsers are running on the same computer and are accessing the same website (in other words, accessing the same IP address on the same port).

How does the operating system recognize which packets are from/for which program?

Does each program have a unique id field in the TCP header? If so, what is the field called?

like image 940
mcjabberz Avatar asked Jul 20 '09 16:07

mcjabberz


People also ask

How does an operating system differentiate between TCP connections?

The two programs are not actually accessing the "same port." For purposes of TCP, a connection is defined by the tuple (src_ip,src_port,dst_ip,dst_port). Thus, the OS can see those are different "connections" and can push the packets to the correct socket objects.

Can 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. That is not specific to Windows, that is part of how TCP works in general.

How does TCP distinguish between different packets in the same session?

TCP connections can detect out of order packets by using the sequence and acknowledgement numbers.

How do multiple clients connect simultaneously to one port?

Multiple clients can connect to the same port (say 80) on the server because on the server side, after creating a socket and binding (setting local IP and port) listen is called on the socket which tells the OS to accept incoming connections.


1 Answers

The two programs are not actually accessing the "same port." For purposes of TCP, a connection is defined by the tuple (src_ip,src_port,dst_ip,dst_port).

The source port is usually ephemeral, which means it is randomly assigned by the OS. In other words:

Program A will have:

(my_ip, 10000, your_ip, 80)

Program B will have:

(my_ip, 10001, your_ip, 80)

Thus, the OS can see those are different "connections" and can push the packets to the correct socket objects.

like image 187
Christopher Avatar answered Nov 02 '22 06:11

Christopher