Internet Transport Layer

The Internet suite has two transport layer protocols, TCP and UDP. Both ride on IP.

TCP corresponds to a connection-oriented, reliable, stream service. Application protocols are built on TCP when:

They need reliability.
They are to operate in a complex network environment with long distances (many hops) between sender and receiver.
They can bear the overhead of connection establishment.
A retransmission scheme to insure reliability makes sense.

Many app protocols use TCP: FTP, TELNET, SMTP, NNTP, etc.

UDP corresponds to a connection-less, unreliable, datagram service. Application protocols are built on UDP when:
They don't care about reliability.
They are intended for a limited, high speed, controlled, small environment (like a LAN).
They send very little data and would suffer with the overhead of connection establishment.
They can use retransmission to replace missing pieces. Synchronous apps like audio and video cannot in general.

Fewer app protocols use UDP: DNS, NFS, SNMP

Addressing

Addressing of a process/host is done via 16 bit port numbers.  This allows more than one app at a time to be using TCP or UDP simultaneously (in effect multiplexing the protocol stack).  A full address for a socket is then an IP address + a port number.

Well-known ports are services that have agreed upon port numbers.  Reserved ports are 1-255, or 1-1023.

Port numbers are 16 bit quantities, so the largest port number is 65,535.

Delivery

Incoming IP packets containing TCP segments or UDP datagrams are de-multiplexed by port number. A queue in the kernel is associated with each port. Data is stored in these queues until the process which is responsible for it reads data from the queue. If no process is reading data from a particular port, an ICMP message is returned to the sender (port unreachable).