4

This is from the book: "Computer Networking - A Top Down Approach" :

One subtle difference between a TCP socket and a UDP socket is that a TCP socket is identified by a four-tuple: (source IP address, source port number, destination IP address, destination port number). Thus, when a TCP segment arrives from the network to a host, the host uses all four values to direct (demultiplex) the segment to the appropriate socket.1

So when I look at TCP headers, such as the one below, I do not see any IP parts in the header.

TCP Header

How does TCP use IP information when passing the data to the correct socket?


1Kurose, J. F., & Ross, K. W. (2013). Computer networking: A top-down approach. Boston, Mass: Pearson.

  • Did any answer help you? If so, you should accept the answer so that the question doesn't keep popping up forever, looking for an answer. – Ron Maupin Jul 25 '17 at 14:15
8

TCP header is located after the IP header, so the TCP/IP stack knows all four (and many more) values.

Those four values are used to form a unique connection ID ("socket"), which is used to recieve and send packets to/from the remote host.

| improve this answer | |
  • Sorry I do not understand, TCP is after IP so in Transport Layer ( TCP layer ) when the data arrives, it does not have the IP information anymore.. The IP header is discarded already? There is only TCP header + payload? – Koray Tugay Feb 21 '14 at 6:35
  • 1
    The TCP/IP stack is not a ISO/OSI college theoretic model. When a packet comes to your computer, it gets parsed, first the destination mac is checked, and the info (src, dst mac, type) is stored in memory; then the IP header is checked, and the values stored, then the TCP header is checked, and those values are also stored. Your computer (kernel, stack,..) does not forget the IP header data, after it moves to the next header, so when you finish with the TCP header, you (your computers), knows all (or most) of the data from layers below. – mulaz Feb 21 '14 at 7:24
  • Ah thanks for the detailed information.. So a TCP socket is not "really" in the Transport layer. It uses information from all layers and a part of the OS. – Koray Tugay Feb 21 '14 at 9:10
1

In IPv4, the protocols are added onto each other. So the IP datagram header you've linked contains just, (among other things not related to this discussion) the IP addresses of the sender and receiver. TCP and UDP build "on top of" IP by adding more header information after the IP header; in that extra information lies the port numbers, (and more) for the two endpoints.

A quick google for "tcp packet header" will show you what the TCP layer adds on after the IP header.

(And the short answer to you Q "how can a TCP socket be identified by IP address?", is that it cannot be.)

| improve this answer | |
  • What does "(And the short answer to you Q "how can a TCP socket be identified by IP address?", is that it cannot be.) this mean? – Koray Tugay Feb 21 '14 at 6:35
  • It means: A TCP socket cannot be identified by just the IP addresses of the end-points. – Craig Constantine Feb 21 '14 at 12:19
  • Who said it was identified by just the IP address? – Koray Tugay Feb 22 '14 at 6:03
  • That's the title of the OP's question: "How can a TCP socket be identified by IP address?" – Craig Constantine Feb 22 '14 at 12:05
  • The question says: "One subtle difference between a TCP socket and a UDP socket is that a TCP socket is identified by a four-tuple" – Koray Tugay Feb 22 '14 at 12:52
0

In addition to the data some metadata must be passed between layers.

When a packet comes in the Internet layer will need to determine if the packet is destined for the local machine and if-so pass it to the appropriate transport layer protocol. When it passes it to the transport layer it will also need to pass the source and destination addresses in addition to the packet contents.

Similarly when a packet goes out the transport layer must tell the internet layer what source and destination addresses to use.

How exactly this metadata is passed is an implementation detail. From the point of view of other systems on the Internet it doesn't matter if a seperate metadata structure is used or if the transport protocol implementations work with the IP header directly.


One subtle difference between a TCP socket and a UDP socket is that a TCP socket is identified by a four-tuple:

This is somewhat misleading, both TCP and UDP sockets can exist both with and without a remote IP/port, though only UDP sockets can pass data without one.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.