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.

0

3 Answers 3

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.

3
  • 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? Feb 21, 2014 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, 2014 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. Feb 21, 2014 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.)

5
  • 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? Feb 21, 2014 at 6:35
  • It means: A TCP socket cannot be identified by just the IP addresses of the end-points. Feb 21, 2014 at 12:19
  • Who said it was identified by just the IP address? Feb 22, 2014 at 6:03
  • That's the title of the OP's question: "How can a TCP socket be identified by IP address?" Feb 22, 2014 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" Feb 22, 2014 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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