Matheion

MA-NET1 · Module 7

Transport services, multiplexing, and UDP

Topics: Process-to-process · Ports · UDP · Checksum

Overview

Session 7 — Transport services, multiplexing, and UDP

Mira now understands what her browser asks for and how names turn into addresses. A new question appears: when a segment of data lands on her laptop, how does it find the right program among the many that are running, such as the browser with three tabs, a music player, and a chat window? And when the Skylark Forecast radar ticker wants speed more than perfection, what does it give up by skipping the safer transport option? This session looks at the transport layer, with a close look at its lighter-weight service, UDP.

By the end you should be able to: describe transport as a process-to-process logical channel on top of host-to-host delivery; tell segments from datagrams; explain multiplexing and demultiplexing; say how UDP sockets are identified (a 2-tuple) and how TCP sockets are identified (a 4-tuple); use well-known ports such as 80 and 53; read the four UDP header fields; explain why an application might choose UDP; and carry out a small checksum calculation, knowing that a checksum can detect some errors but never repairs them.

Scroll Core ideas → worked examples → Quiz. The checksum example uses short binary numbers, so a pencil and some paper for lining up columns will help. Plan on roughly 60–70 minutes.

Concepts

Core ideas — transport, ports, and UDP

Up to now we have talked as if messages simply appear at the right program. In this lesson we look at the layer that makes that true: the transport layer. It sits directly beneath the applications we studied (the web, mail, DNS) and directly above the layer that moves packets between machines. We follow Mira again, whose laptop at Skylark Forecast runs many network programs at once. As usual, every address, port, and bit pattern here is invented for practice. Take it slowly; the ideas are simple, but the vocabulary is easy to tangle.


Two kinds of delivery, and what lies between them

Imagine two buildings in different cities. The postal service can carry a parcel from one building to the other, but it knows nothing about which person inside the building should open it. Inside each building, a mailroom clerk reads a name on the parcel and carries it to the right desk.

Computer networks split the same work in two. The network layer, whose address is the IP address, delivers data from one host to another host. It is the postal service. The transport layer extends that delivery from host to process: it gets the data to the right program on the destination host. The transport layer is the mailroom clerk.

This is why we describe transport as providing a logical communication channel between application processes. "Logical" means the two processes behave as if they had a direct connection, even though the actual route crosses many routers that know nothing about processes. Only the end hosts run transport software. Routers in the middle look at IP addresses and forward; they never open the transport message inside.


Segments, datagrams, and what they are called

Transport protocols wrap the application's data with a small header of their own. The resulting package has a name that depends on the protocol.

  • For TCP, the reliable, connection-oriented service, the package is called a segment.
  • For UDP, the lightweight, connectionless service, the package is called a datagram.

The word packet is a more general name for a unit that travels through the network layer. So the layering looks like this: the application produces a message; the transport layer adds its header and creates a segment or datagram; the network layer adds an IP header and creates a packet; and so on downward. The sentence "the segment is carried inside a packet" is perfectly correct.

Common confusion: people often use "packet" for everything, and in casual speech that is fine. In a careful description, though, "segment" and "datagram" tell you which transport protocol you mean, so it is worth using them properly.


Multiplexing and demultiplexing

A laptop may have a browser with three tabs open, a chat program, and a small tool polling the Skylark radar. All of them send and receive data through the same network connection and share one IP address. How does an arriving piece of data find its way to the right one? The answer is a pair of complementary jobs.

Multiplexing happens at the sender. The transport layer gathers outgoing data from several sockets, one for each process, wraps each piece with a header that says which socket it came from and which socket it is for, and passes everything downward to be sent. Think of the mailroom clerk collecting letters from many desks and writing sender and recipient desk numbers on each envelope.

Demultiplexing happens at the receiver. When a segment arrives, the transport layer reads the header fields, works out which socket the segment belongs to, and passes the data up to that socket. This is the clerk reading the desk number and delivering the letter.

The information that makes this possible is the pair of port numbers in each header: a source port and a destination port. Each is a 16-bit number, so ports range from 0 to 65,535. The destination port, together with the destination IP address, identifies the receiving socket. The source port tells the receiver where to send a reply.

Well-known ports are those from 0 to 1,023, set aside by convention for standard services. Two you will meet constantly are 80 for HTTP and 53 for DNS. When Mira's browser contacts api.skylark.example, it sends to destination port 80 on the server. Her browser's own end uses a high-numbered port picked by the operating system, such as 51200, which is called an ephemeral port.


How a UDP socket is identified: the 2-tuple

UDP is simple, and so is its demultiplexing. A UDP socket is identified by just two things: the destination IP address and the destination port number. That pair is the 2-tuple.

The consequence is easy to state and surprising the first time you hear it: if two UDP datagrams arrive with the same destination IP address and destination port, they are handed to the same socket, no matter where they came from. Suppose a resolver listens on port 53. A datagram from Mira's laptop and a datagram from her phone, both addressed to that resolver's port 53, are delivered to the same socket. The two different source ports and addresses do not create separate sockets; they are simply passed along with the data so that the application knows where to send each reply.

This is perfectly adequate for applications that handle each request as a self-contained little question and answer.

Common confusion: the source IP address and source port do not choose the UDP socket. They are information for the application. Only the destination pair chooses the socket.


How a TCP socket is identified: the 4-tuple

TCP sockets are identified more finely, by four things: source IP address, source port, destination IP address, destination port. That is the 4-tuple, and it makes sense because TCP is connection-oriented: a connection exists between exactly two endpoints, and each connection gets its own socket at the server.

Consider a web server listening on port 80. It has one listening socket that waits for new connection requests. When a connection is established, the server creates a new socket dedicated to that connection. Two connections from Mira's laptop, one per browser tab, share the same source IP address and the same destination (server address and port 80), but they use different source ports, such as 51200 and 51201. Different source port means a different 4-tuple, hence a different socket. A third connection from another machine that happens to choose the same source port, 51200, still differs in the source IP address and is a different 4-tuple again.

So a busy web server can handle thousands of simultaneous clients, all talking to the same server port 80, and the transport layer keeps them separate using the full 4-tuple.

Keep the contrast clear:

  • UDP: destination IP + destination port. Many senders, one socket.
  • TCP: source IP + source port + destination IP + destination port. Each connection, its own socket.

The UDP header

A UDP datagram's header is only 8 bytes long, made of four fields of 2 bytes (16 bits) each.

  1. Source port. The sender's port, which tells the receiver where replies should be sent.
  2. Destination port. The port of the receiving process, such as 53 for DNS.
  3. Length. The total length of the datagram in bytes, header plus data. A datagram with a 30-byte payload has length 8 + 30 = 38.
  4. Checksum. A small value used by the receiver to detect whether the datagram was damaged on the way.

That is the whole header. There are no sequence numbers, no acknowledgments, no connection state, and no window. UDP's service on top of IP is nearly nothing: it adds demultiplexing (the ports) and a minimal error check (the checksum). Nothing else. It does not guarantee delivery, order, or protection from duplicates.


Why an application might choose UDP

At first glance UDP looks like the poorer option: why would anyone choose a service that promises less? Several practical reasons exist.

Finer control over when data is sent. TCP may slow down or delay sending because of its congestion and reliability machinery. With UDP, the datagram goes into the network as soon as the application hands it over. For a live radar ticker, speed of the latest update matters more than getting every old one.

No connection setup. TCP needs a handshake before data flows, which costs a round trip. DNS, for example, wants to ask a one-datagram question and get a one-datagram answer; adding a handshake would roughly double the delay.

No connection state. The server does not need to remember buffers and counters for each client, so a single machine can serve many more clients.

Small header. Eight bytes instead of the larger TCP header means less overhead, which matters for tiny messages.

Applications can add what they need. If an application wants some reliability on top of UDP, its own code can add acknowledgments and retransmissions, tailored to its needs. Several modern protocols are built exactly that way.

Typical users of UDP include DNS queries, live audio and video, online games, and small status broadcasts. A forecast file download, on the other hand, would go over TCP, because losing the middle of a file is unacceptable.

Common confusion: "UDP is unreliable" does not mean "UDP usually loses data." It means UDP makes no promise. On a healthy network, most datagrams arrive.


The checksum: detect, never correct

Bits can occasionally be flipped by electrical noise or by a faulty device on the path. The checksum is a cheap way to notice some of that damage. Here is the method, described for short words first.

The sender treats the datagram's contents as a sequence of 16-bit words. (To keep arithmetic small, our worked example will use 8-bit words; the method is identical.) It then:

  1. Adds all the words together as binary numbers.
  2. If an addition produces a carry out of the leftmost bit, that carry is wrapped around: removed and added back at the right end. This is called one's-complement addition.
  3. Takes the one's complement of the final sum: flip every bit, so each 0 becomes 1 and each 1 becomes 0.
  4. Places the result in the checksum field.

The receiver adds up all the words, including the checksum, using the same wraparound rule. If nothing was damaged, the result is a word of all ones. If any bit of the result is 0, the receiver knows that something changed, and the datagram is dropped, or passed up with a warning, depending on the implementation.

Two limits deserve emphasis.

First, the checksum detects, but it does not correct. The receiver learns "something is wrong" but not what or where. Recovery, if there is any, is up to a higher layer.

Second, the checksum is not foolproof. Some combinations of errors cancel each other out in the sum. For example, if one bit flips from 0 to 1 in one word, and the bit in the same column flips from 1 to 0 in another word, the sum does not change and the damage passes unnoticed. The worked examples show this concretely. In practice such coincidences are rare, but they exist.

A natural question: since lower layers also check for errors, why does UDP bother? The answer is that there is no guarantee every link on the path does error checking, and errors can also be introduced inside a router's memory. Checking at the end hosts is a cheap safety net at the one layer both ends control.


What to carry forward

Transport turns host-to-host delivery into process-to-process delivery, using port numbers to multiplex and demultiplex. UDP sockets are chosen by destination IP and port alone; TCP sockets by the full four values. A UDP header contains two ports, a length, and a checksum, and nothing more, which is exactly why some applications prefer it. The checksum can raise an alarm about some errors, but it cannot repair them and can occasionally miss them. The next sessions take up how a transport protocol can build reliability on top of an unreliable network.

Examples

Worked examples — sockets, headers, and a tiny checksum

These five short stories follow Mira as she investigates how data finds the right program. Every address, port, and bit pattern is invented for practice, and the addresses come from ranges that are reserved for documentation, so they will never point at a real machine. Work each one with a pencil if you can. The Interpretation paragraphs are where the idea is named, so read them even if the arithmetic felt easy.


Example A — Three UDP datagrams, two sockets

Scene. A DNS resolver run by BayLink Access has the address 10.20.0.53. It has one UDP socket open for port 53 (the well-known DNS port) and a second UDP socket open for a monitoring tool on port 5300. Three datagrams arrive at the resolver, listed with their header information:

| Datagram | Source IP : port | Destination IP : port | |---|---|---| | 1 | 192.168.1.20 : 50034 (Mira's laptop) | 10.20.0.53 : 53 | | 2 | 192.168.1.31 : 41877 (Mira's phone) | 10.20.0.53 : 53 | | 3 | 192.168.1.20 : 50034 (Mira's laptop) | 10.20.0.53 : 5300 |

Which socket does each datagram go to, and how many distinct sockets are used?

Calculation. For UDP, the socket is chosen by the 2-tuple of destination IP and destination port only.

  1. Datagram 1: destination 2-tuple is (10.20.0.53, 53).
  2. Datagram 2: destination 2-tuple is (10.20.0.53, 53). It is the same pair as datagram 1, even though the source IP and source port differ.
  3. Datagram 3: destination 2-tuple is (10.20.0.53, 5300). That is a different pair, even though the sender is exactly the same as in datagram 1.
  4. Distinct sockets used: 2. Datagrams 1 and 2 share the first one; datagram 3 uses the second.

Interpretation. The laptop and the phone do not get separate sockets at the resolver. Their different source information is simply delivered along with the data, so the resolver's program knows where to address each answer: the reply to datagram 1 goes to 192.168.1.20 port 50034, and the reply to datagram 2 goes to 192.168.1.31 port 41877. Datagram 3 shows the other side of the coin: the same sender reaches a different socket purely because it chose a different destination port.

Takeaway. UDP demultiplexing looks only at destination IP and destination port.


Example B — The same web server, four arriving segments

Scene. The Skylark server at 203.0.113.40 has a listening socket on TCP port 80. Mira opens two tabs, and her colleague Teo opens one. Four segments arrive, all addressed to 203.0.113.40 port 80:

| Segment | Source IP : port | |---|---| | s1 | 198.51.100.7 : 51200 (Mira, tab 1) | | s2 | 198.51.100.7 : 51201 (Mira, tab 2) | | s3 | 198.51.100.88 : 51200 (Teo) | | s4 | 198.51.100.7 : 51200 (Mira, tab 1 again) |

How many connection sockets does the server end up with, and which segments share one?

Calculation. For TCP, a connection socket is identified by the 4-tuple: (source IP, source port, destination IP, destination port). The destination part, (203.0.113.40, 80), is the same for all four, so only the source part varies.

  1. s1: (198.51.100.7, 51200, 203.0.113.40, 80). New 4-tuple, so connection socket 1.
  2. s2: (198.51.100.7, 51201, ...). The source port differs from s1, so connection socket 2.
  3. s3: (198.51.100.88, 51200, ...). The source port equals s1's, but the source IP differs. A different 4-tuple, so connection socket 3.
  4. s4: (198.51.100.7, 51200, ...). This is exactly the same 4-tuple as s1, so it goes to connection socket 1.
  5. Number of connection sockets: 3, with s1 and s4 sharing socket 1.

Interpretation. Notice how the server tells tab 1 and tab 2 apart even though they come from the same laptop: by the source port. And notice that Teo's source port, 51200, happens to equal Mira's. That collision causes no trouble, because the source IP address differs. Compare with Example A, where a different source did not create a different socket. The extra fields in the 4-tuple are the price of being connection-oriented. Finally, remember the listening socket: it does not carry any of this data. It only accepts new connections, and each accepted connection receives its own socket.

Takeaway. TCP demultiplexing uses all four values. Two connections differ if any one of them differs.


Example C — Building a UDP header by hand

Scene. Mira's laptop sends a DNS query to the resolver at port 53. The laptop's operating system chose ephemeral source port 50034. The query payload is 30 bytes long. Fill in the four UDP header fields, and state the size of the datagram.

Calculation.

  1. Source port: 50034. In hexadecimal that is C372.
  2. Destination port: 53, the well-known DNS port. In hexadecimal, 0035.
  3. Length: header plus payload = 8 + 30 = 38 bytes. In hexadecimal, 0026.
  4. Checksum: computed last, from the header and payload together. (We carry out a miniature version of this computation in Example D.)
  5. Total header size: 4 fields × 2 bytes = 8 bytes. Total datagram: 8 + 30 = 38 bytes, which matches the Length field.

Laid out as it appears on the wire:

| source port  | dest port    |
|    50034     |      53      |
| length       | checksum     |
|     38       |   (computed) |
|           payload            |
|          (30 bytes)          |

Interpretation. Observe how little is in this header: no sequence number, no acknowledgement, no sign that a connection exists. When the resolver replies, it will swap the ports: the reply's source port will be 53 and its destination port will be 50034. That is how Mira's operating system knows to hand the reply to the program that asked. A quick sanity check is worth remembering: Length must always be at least 8, because even an empty datagram contains the header.

Takeaway. A UDP header is 8 bytes: source port, destination port, length (header + data), checksum.


Example D — A miniature checksum: compute, detect, and miss

Scene. To keep the arithmetic small, we pretend a datagram's content is just two 8-bit words (the real thing uses 16-bit words, but the method is identical). Mira's tool sends:

  • Word 1: 10110011
  • Word 2: 01101110

Part 1 — the sender computes the checksum.

  1. Add the two words as binary numbers: 10110011 + 01101110. In decimal that is 179 + 110 = 289, which needs 9 bits: 1 00100001.
  2. The leading 1 is a carry out of the leftmost bit. Wrap it around: remove it and add 1 at the right end. 00100001 + 1 = 00100010.
  3. Take the one's complement of 00100010: flip every bit, giving 11011101.
  4. The checksum is 11011101, and the sender transmits the two words plus this checksum.

Part 2 — the receiver checks an undamaged datagram.

  1. Add all three words with wraparound. We already know the first two give 00100010.
  2. 00100010 + 11011101 = 11111111, with no carry.
  3. The result is all ones, so the receiver concludes that no error was detected.

Part 3 — one bit gets damaged in transit.

Suppose noise flips the third bit from the left of word 1, so it arrives as 10010011 instead of 10110011.

  1. Add the received words: 10010011 + 01101110 = 147 + 110 = 257 = 1 00000001.
  2. Wrap the carry: 00000001 + 1 = 00000010.
  3. Add the checksum: 00000010 + 11011101 = 11011111.
  4. The result contains zeros, so the receiver detects that something went wrong.

Part 4 — two bits get damaged and nobody notices.

Now suppose that, in the same column (the fourth from the left, worth 16), word 1 flips from 1 to 0 and word 2 flips from 0 to 1. Word 1 arrives as 10100011 and word 2 as 01111110.

  1. Add: 10100011 + 01111110 = 163 + 126 = 289 = 1 00100001.
  2. Wrap the carry: 00100010. This is exactly the same sum as in Part 1.
  3. Add the checksum: 00100010 + 11011101 = 11111111.
  4. All ones: the receiver accepts a damaged datagram.

Interpretation. Three lessons sit in this one example. First, the checksum works by making an undamaged total come out as all ones, which is an easy thing for the receiver to test. Second, in Part 3 it raised an alarm, but notice that it did not say which bit was wrong or what the right value should be: the checksum detects, it does not correct. Third, in Part 4 one extra 1 and one missing 1 in the same column cancelled in the sum, so the error slipped through. The checksum is a useful safety net but not a guarantee.

Takeaway. Add with wraparound, flip the bits, send. At the receiver, add everything: all ones means "no error detected," anything else means "damaged."


Example E — Choosing UDP or TCP for three Skylark features

Scene. Skylark Forecast has three features, and the team must decide which transport each should use. Mira collects some invented numbers. The round-trip time to the relevant server is 18 ms. The minimum TCP header is 20 bytes; the UDP header is 8 bytes.

Calculation, feature 1: a DNS lookup of 30 bytes of data.

  1. Over UDP: send the query, receive the answer. Time ≈ 1 RTT = 18 ms. Bytes on the wire for the query: 8 + 30 = 38.
  2. Over TCP: handshake first (1 RTT), then query and answer (1 RTT). Time ≈ 2 × 18 = 36 ms. Bytes for the query segment: 20 + 30 = 50.
  3. Header share of the query: UDP 8 / 38 ≈ 21 %; TCP 20 / 50 = 40 %.

Calculation, feature 2: a radar ticker that sends a small status datagram every 2 seconds.

  1. Each update replaces the previous one. If one is lost, the next arrives in 2 s with fresher information.
  2. A reliable transport would insist on re-sending the old update, which is already out of date, and may hold back newer ones while it does so.
  3. So here the application prefers the freshest data and tolerates occasional loss: UDP.

Calculation, feature 3: downloading a 40 MB offline map bundle.

  1. If any chunk is lost or reordered, the bundle is corrupted or unusable.
  2. The download is elastic: it simply uses whatever speed is available.
  3. So the application needs complete, in-order delivery: TCP.

Interpretation. The DNS comparison shows two of UDP's advantages together: no handshake halves the delay (18 ms against 36 ms), and the smaller header makes tiny messages cheaper. The ticker shows the "freshness beats completeness" argument. The map bundle shows when reliability is non-negotiable. None of these choices says one protocol is better in general; each application matched its needs to what the transport offers. Note also that a team could choose UDP for the ticker and still add its own sequence numbers, so the receiving app could ignore updates that arrive out of order.

Takeaway. Choose UDP when speed, freshness, or minimal overhead matter more than guaranteed delivery. Choose TCP when every byte must arrive, in order.

Practice check — transport, ports, and UDP

Multiple-choice check from the Matheion app lesson. Pick an answer for each question, then submit.

  1. 1.Which statement best separates the job of the network layer from the job of the transport layer?

  2. 2.Which pairing of names is correct?

  3. 3.Two UDP datagrams arrive at `10.20.0.53`, both addressed to port 53. One came from `192.168.1.20:50034` and the other from `192.168.1.31:41877`. What happens?

  4. 4.Mira has two browser tabs open to the same server on port 80, using her laptop's single IP address. How does the server's transport layer keep the two connections apart?

  5. 5.A UDP datagram carries 52 bytes of application data. What value does the Length field hold?

  6. 6.Which of these is a good reason for an application to choose UDP?

  7. 7.Using 8-bit words and one's-complement addition with wraparound, what is the checksum of the two words `11100101` and `10011001`?

  8. 8.A receiver adds all words plus the checksum and gets `11101111`. What should it conclude?