tags:

views:

394

answers:

5

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?

A: 

By port number.

Will
-1? Really? What, I'm wrong?
Will
why was this voted down? It's somewhat lacking in detail but it's not wrong and the link to wikipedia would be useful if it wasn't a rather confusing wikipedia page
Mark Baker
Hurf​ .
Will
Upvoted for remarkable brevity.
NateDSaint
Not wrong, but not helpful either.
John Kugelman
+3  A: 

the source port number will be different even if the destination port number is the same. the kernel will associate the source port number with the process.

QAZ
+22  A: 

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.

Christopher
the correct answer
Brian R. Bondy
It's also worth noting that when you're performing socket operations while developing (say, opening a socket connection in Java), the source port is automatically assigned by the system.
Jack Leow
I did mention that: "The source port is usually ephemeral, which means it is randomly assigned by the OS" :-)
Christopher
Thanks man! I was told that there was some field in the TCP header... I guess I should stop listening to them then. :)
mcjabberz
It doesn't have to be assigned by the system, you can request a specific source port with bind.
Robert S. Barnes
Hence Christopher's choice of the word "usually" in the description of the source port choice.
lavinio
And when it is assigned by the system, its often not random. Sequential assignment of ephemeral source ports is not at all uncommon.
caf
+3  A: 

When the client opens a connection to destination port 80, it uses an arbitrary unused source port on the local machine, say 17824. The web server then responds to that client by sending packets to destination port 17824.

A second client will use a second unused port number, say 17825, and so the two sockets' packets will not be mixed up since they'll use different port numbers on the client machine.

John Kugelman
Just to note that it doesn't have to use an ephemeral source port. It can request to bind a specific sorce port.
Robert S. Barnes
A: 

Christopher's answer is partially correct.

Programs A and B actually have a handle to a socket descriptor stored in the underlying OS's socket implementation. Packets are delivered to this underlying socket, and then any process which has a handle to that socket resource can read or write it.

For example, say you are writing a simple server on a Unix like OS such as Linux or Mac OSX.

Your server accepts a connection, at which point a connection consisting of

( src IP, src Port, dest IP, dest Port )

comes in to existence in the underlying OS socket layer. You then fork a process to handle the connection - at this point you now have two processes with handles to the socket both of which can read / write it.

Typically ( always ) the original server will close it's handle to the socket and let the forked process handle it. There are many reasons for this, but the one that is not always obvious to people is that when the child process finishes it's work and closes the socket the socket will stay open and connected if the parent process still has an open handle to it.

Robert S. Barnes
I think few modern server-side programs `fork` and `exec` for each incoming connection. A single thread can handle many simultaneous connections, and is way more efficient. This comment describes one particular method of implementation.
lavinio
Actually, this comment is trying to clarify the misunderstanding that the socket belongs exclusively to one process and that the OS "delivers" packets to a particular process. The socket belongs to the OS and the process has access to that underlying OS resource.
Robert S. Barnes