# 50.012 Lecture 7
## Transport services and Protocols
* provide logical communication between app processes running on different hosts.
* Transport protocols run in end system.
### Transport vs. Network Layer
Network: Logical communication between **hosts** (how the packet goes from one host to another). e.g. IP protocol
Transport: Logical communication between **processes**.
## Internet Transport-Layer Protocol
Reliable, in-order delivery (TCP)
* Congestion control
* Flow control
* Connection setup
Unreliable, unordered delivery (UDP)
* No-frills extension of "best-effort" IP
Transport layer does not have:
* Delay guarantees
* Bandwidth guarantees
## Multiplexing/Demultiplexing

### Multiplexing
Happens at the sender, invoked when application needs to send a message. It handles data from multiple sockets, adds transport header which will be used for demuxing.
It is possible for a process to have a thread pool to manage multiple sockets.
### Demultiplexing

1. Host receives IP datagrams (packet at IP layer)
* Each datagram has source and destination IP addresses
* Each datagram carries one transport-layer segment.
* Each segment has source, destination port numbers.
2. Host uses IP addresses and port numbers to resolve where the packet should be delivered to.
The first 32 bits of TCP or UDP datagram is the same.
#### Connectionless Demuxing
Socket has a host-local port number. When creating datagram to send into UDP socket, we must specify the destination IP address and port number.
When host receives UDP segment, it checks the destination port number in the segment and direct UDP segment to the socket with that port number.
IP datagrams with the same destination port number but different source IP addresses and/or source port numbers will be directed to the same socket at destination.
Server port number is usually pre-defined (and use commonly used number), client socket can be decided by the OS.

Besides the destination port, the IP address and port number of the sender need to be sent to the application layer as well so that the receiver knows where to send the response to.

### Connection-oriented Demux

TCP socket is identified by 4-tuple:
* Source IP Address
* Source port number
* Destination IP Address
* Destination Port Number
Due to the higher number of states that the TCP server has to maintain, it is more resource hungry.
The demux in the receiver uses all four values to direct the segment to appropriate socket.
Web servers have different sockets for each connecting client. Non-persistent HTTP will have different socket for each request, whereas persistent HTTP will have the same socket serving a particular client.
Purely server performance-wise, non-persistent is better since it's not "bound" to one client, so can serve more clients at a time.
When there's a lot of request/response, persistence is better for server since it can reuse the same socket to receive/send request/response.
### UDP
UDP segments may be lost and delivered out-of-order to the app.
It is connectionless:
* No handshaking between UDP sender and receiver.
* Each UDP segment handled independently of others.
UDP use cases:
* Streaming multimedia apps (loss tolerant, rate sensitive)
* DNS (We require this service to be as fast as possible)
* SNMP
#### UDP: Segment Header

#### UDP Checksum
The purpose of this checksum is to detect errors (e.g. flipped bits) in transmitted segment.
Sender:
* Treat segment contents (including header fields) as sequence of 16-bit integers
* Checksum: addition (one's complement sum) of segment contents
* Sender puts checksum value into UDP checksum field.
Receiver:
* Compute checksum of received segment
