3rd Edition: Chapter 2
Chapter 2 Application Layer
2.7 socket programming with UDP and TCP
Application Layer
2-*
Chapter 3 Transport Layer
3.1 transport-layer services
These slides contains materials from the text and material
from Professor McKinley’s lecture slides.
Application Layer
Application Layer
2-*
Socket programming
goal: learn how to build client/server applications that communicate using sockets
Internet
controlled
by OS
controlled by
app developer
transport
application
physical
link
network
process
transport
application
physical
link
network
process
socket
Application Layer
Application Layer
2-*
Socket programming
application layer: we/programmers write network programs that run on end systems
socket: door between application process and end-end-transport protocol
transport layer: provide logical communication between app processes
Internet
controlled
by OS
controlled by
app developer
transport
application
physical
link
network
process
transport
application
physical
link
network
process
socket
Application Layer
application layer: We write network programs that run on end systems.
socket: door between application process and end-end-transport protocol
process sends/receives message to/from its socket
The OS manages the lower layers and the internet infrastructure delivers the message from sending process to the socket at receiving process.
*
Data loss
some apps (e.g., audio) can tolerate some loss
other apps (e.g., file transfer, telnet) require 100% reliable data transfer
Timing
some apps (e.g., Internet telephony, interactive games) require low delay to be “effective”
Transport Services
Throughput
some apps (e.g., multimedia) require minimum amount of throughput to be “effective”
other apps (“elastic apps”) make use of whatever throughput they get
Security
Encryption, data integrity, …
Communication in real-world is complicated.
*
Application Layer
2-*
Transport service requirements: common apps
application
file transfer
e-mail
Web documents
real-time audio/video
stored audio/video
interactive games
text messaging
data loss
no loss
no loss
no loss
loss-tolerant
loss-tolerant
loss-tolerant
no loss
throughput
elastic
elastic
elastic
audio: 5kbps-1Mbps
video:10kbps-5Mbps
same as above
few kbps up
elastic
time sensitive
no
no
no
yes, 100’s msec
yes, few secs
yes, 100’s msec
yes and no
Application Layer
*
Transport Layer
3-*
Transport services and protocols
provide logical communication between app processes running on different hosts
transport protocols run in end systems
send side: breaks app messages into segments, passes to network layer
rcv side: reassembles segments into messages, passes to app layer
more than one transport protocol available to apps
Internet: TCP and UDP
application
transport
network
data link
physical
logical end-end transport
application
transport
network
data link
physical
Transport Layer
So, at the application layer, we (the programmer/user) can send a big chunk of data. The underlying transport protocol will take care of the rest. It breaks …
*
Application Layer
2-*
Internet transport protocols services
TCP service:
reliable transport between sending and receiving process
flow control: sender won’t overwhelm receiver
congestion control: throttle sender when network overloaded
does not provide: timing, minimum throughput guarantee, security
connection-oriented: setup required between client and server processes
UDP service:
unreliable data transfer between sending and receiving process
does not provide: reliability, flow control, congestion control, timing, throughput guarantee, security, orconnection setup,
Q: why bother? Why is there a UDP?
Application Layer
*
Application Layer
2-*
Internet apps: application, transport protocols
application
e-mail
remote terminal access
Web
file transfer
streaming multimedia
Internet telephony
application
layer protocol
SMTP [RFC 2821]
Telnet [RFC 854]
HTTP [RFC 2616]
FTP [RFC 959]
HTTP (e.g., YouTube),
RTP [RFC 1889]
SIP, RTP, proprietary
(e.g., Skype)
underlying
transport protocol
TCP
TCP
TCP
TCP
TCP or UDP
TCP or UDP
Application Layer
*
Transport Layer
3-*
Transport vs. network layer
network layer: logical communication between hosts
transport layer: logical communication between processes
relies on, enhances, network layer services
12 kids in Ann’s house sending letters to 12 kids in Bill’s house:
hosts = houses
processes = kids
app messages = letters in envelopes
transport protocol = Ann and Bill who demux to in-house siblings
network-layer protocol = postal service
household analogy:
Transport Layer
Application Layer
2-*
Addressing processes
To send/receive, process must have some identifier
end host (house) have unique 32-bit IP address
Windows: ipconfig, OSX/*nix: ifconfig to get IP addr from command prompt
Multiple processes running on the same end host
(Multiple kids living in the same house)
IP is not sufficient for identifying processes on the same host.
port number for different processes (kids) on the same host
common port numbers:
HTTP server: 80, mail server: 25, SSH: 22
Application Layer
Application Layer
2-*
Socket API
socket API
key concept: make network access look much like file access
explicitly created, used, and released by processes.
socket is a kernel data structure
accessed via a file descriptor
a bi-directional endpoint of communication
accesses transport functions directly
three socket types
UDP (User Datagram Protocol): Unreliable datagram
TCP (Transmission Control Protocol): reliable, byte stream oriented
RAW: access IP directly
Application Layer
*
Application Layer
2-*
socket() system call
socket() creates a socket.
socket() takes three parameters
socket family: AF_INET for internet.
type of communication
TCP: SOCK_STREAM, UDP: SOCK_DGRAM, RAW: SOCK_RAW,
specific protocol to use, (often implicit)
socket() returns a file descriptor
a socket is created without name
socket name includes protocol family, IP address and port
Sockets must have a name to receive data
name is bound to the socket later, using bind()
Application Layer
*
Application Layer
2-*
Socket programming basics
server must be running before clients can send anything to it.
any process must have a socket through which it sends and receive messages.
socket is locally (in a host) identified with a port number
client needs to know server IP address and port number
Application Layer
*
Application Layer
2-*
Socket programming with UDP
UDP: no “connection” between client & server
no handshaking before sending data
sender explicitly attaches IP destination address and port to each packet
receiver can extract sender IP address and port from received packet
UDP: transmitted data may be lost or received out-of-order
Application viewpoint:
UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server
Application Layer
Application Layer
2-*
Example app: UDP server/client
A UDP server that receives message and echoes similar message to the UDP client
Client
Send: “HELLO”
Respond: “I got your message: HELLO”
User input some character into
the buffer: “HELLO”
Server
Server receives the message and insert
some other characters
Send: “TEST”
Respond: “I got your message: TEST”
User input some character into
the buffer: “TEST”
Server receives the message and insert
some other characters
This is a simple “application layer protocol”.
Application Layer
Application Layer
2-*
A UDP server that receives message and echoes similar message to the UDP client
Example app: UDP server
Application Layer
UDP socket creation
*
Application Layer
2-*
Example app: UDP server
Application Layer
Setting socket name, let the OS choose the port, obtain the port number assigned by the OS
*
Application Layer
2-*
Example app: UDP server
Application Layer
recvfrom and sendto
*
Application Layer
2-*
Example app: UDP client
Application Layer
Gethostbyname
Create socket
Set socket name
*
Application Layer
2-*
Example app: UDP client
Application Layer
Sendto
*
Application Layer
2-*
Example app: UDP client
Application Layer
Recvfrom and recv
*
Application Layer
2-*
Socket programming with TCP
client must contact server
server process must first be running
server must have created socket (door) that welcomes client’s contact
client contacts server by:
Creating TCP socket, specifying IP address, port number of server process
when client creates socket: client TCP establishes connection to server TCP
when contacted by client, server TCP creates new socket for server process to communicate with that particular client
allows server to talk with multiple clients
source port numbers used to distinguish clients (more in Chap 3)
TCP provides reliable, in-order
byte-stream transfer (“pipe”)
between client and server
application viewpoint:
Application Layer
Application Layer
2-*
Example app: TCP server/client
A TCP server that receives message and echoes to the TCP client
Client
Send: “HELLO”
Respond: “I got your message: HELLO”
User input some character into
the buffer: “HELLO”
Server
Server receives the message and insert
some other characters
Send: “TEST”
Respond: “I got your message: TEST”
User input some character into
the buffer: “TEST”
Server receives the message and insert
some other characters
This is a simple “application layer protocol”.
Application Layer
Application Layer
2-*
Simple multiprocess program
fork() creates a child process by duplicating the parent/calling process
The child process has a pid == 0
Both parent and child process execute the next instruction
Both parent and child process has the same code segment, but they are independent to each other.
Child process has its own address space and has its own copy of the variables
We can create a multiprocess program that
Parent process keeps listening for incoming connections
For each connection, parent process forks a child process to handle the connection
Application Layer
Application Layer
2-*
Example app: TCP server
Application Layer
UDP socket creation
*
Application Layer
2-*
Example app: TCP server
Application Layer
UDP socket creation
*
Application Layer
2-*
Example app: TCP server
Application Layer
UDP socket creation
*
Application Layer
2-*
Example app: TCP client
Application Layer
UDP socket creation
*
Application Layer
2-*
Example app: TCP client
Application Layer
UDP socket creation
*
Application Layer
2-*
Example app: TCP client
Application Layer
UDP socket creation
*
htons() and ntohs()
Application Layer
2-*
different architectures use different ordering of bytes in a word of memory
big-endian: most significant byte (MSB) is stored first
little-endian: MSB stored last (our CSE machines.)
The Internet protocol avoids this difference and uses big-endian to accommodate different machines
If you try to output sin_port without taking care of this issue, the number printed might not be the actual port number.
htons(): host to network short, it detects the hosts endianness and convert the port number to internet byte order
ntohs(): network to host short, it converts the port number in internet byte order to host byte order
Use htons() and ntohs() when assigning/obtaining port numbers
Application Layer