Lecture 11
Network Programming
Acknowledgement: These slides are based on the textbook
(Computer Systems: A Programmer’s Perspective) and its slides. 1
In this lecture, we will study:
basic concepts in computer networking
tools/commands for networking
low-level functions for networking in the C language
but you are not expected to write networking programs yet
More details will be covered in another subject:
COMP2322 Computer Networking
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 2
A Client-Server Transaction
Many network applications are based on the client-
server model:
A server process & multiple client processes
Server manages some resource and provides service to clients
Server activated by request from client
1. Client sends request
Client Server
process process Resource
4. Client 3. Server sends response 2. Server
handles handles
response request
Note: clients and servers are processes running on hosts
(can be the same or different hosts)
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 3
Outline
Networks
Global IP Internet
The Sockets Interface
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 4
Hardware Organization of a Network Host
CPU chip
register file
ALU
system bus memory bus
I/O main
MI
bridge memory
Expansion slots
I/O bus
USB graphics disk network
controller adapter controller adapter
mouse keyboard monitor
disk network
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 5
Computer Networks
A network is a hierarchical system of boxes and wires
organized by geographical proximity
SAN (System Area Network) spans cluster or machine room
LAN (Local Area Network) spans a building or campus
WAN (Wide Area Network) spans country or world
An internetwork (internet) is an interconnected set of
networks
The Global IP Internet (uppercase “I”) is the most famous
example of an internet (lowercase “i”)
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 6
LANs and internets
Simplified view of a LAN
A collection of hosts attached to a single wire
host host ... host
An internet (lower case): multiple LANs connected
by routers
host host ... host host host ... host
LAN 1 LAN 2
router router router
WAN WAN
LAN 1 and LAN 2 might be different and incompatible
(e.g., Ethernet, Fibre Channel, 802.11*, T1-links, DSL, …)
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 7
Logical Structure of an internet
host
router
host router router
router
router router
Ad hoc interconnection of networks
No particular topology
Send packets from source to destination through networks
Router forms bridge from one network to another
Different packets may take different routes
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 8
How to send bits across incompatible
LANs and WANs?
Solution: an internet protocol running on hosts and routers
Protocol is a set of rules that governs how hosts and routers should
cooperate when they transfer data from network to network
It provides a naming scheme
It defines a uniform format for host addresses
Each host (and router) is assigned at least one of these internet addresses
that uniquely identifies it
It provides a delivery mechanism
It defines a standard transfer unit (packet) header payload
Packet consists of header and payload
Header: contains info such as packet size, source and destination
addresses
Payload: contains data bits sent from source host
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 9
Transferring internet Data Via Encapsulation
LAN1 Host A Host B LAN2
client server
(1) data (8) data
protocol protocol
internet packet software software
(2) data PH FH1 (7) data PH FH2
LAN1 frame
LAN1 LAN2
adapter adapter
Router
(3) data PH FH1 (6) data PH FH2
LAN1 LAN2
adapter adapter
LAN2 frame
(4) data PH FH1 data PH FH2 (5)
PH: Internet packet header protocol
FH: LAN frame header software
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 10
Other Issues
In the area “computer networking”, we study the
following important issues:
How do routers know where to forward frames?
How are routers informed when the network
topology changes?
What if packets get lost?
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 11
Outline
Networks
Global IP Internet
The Sockets Interface
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 12
Global IP Internet (upper case)
Based on the TCP/IP protocol family
IP (Internet Protocol) : it provides basic naming scheme and
unreliable delivery capability of packets (datagrams) from host-to-host
TCP (Transmission Control Protocol): it uses IP to provide reliable
byte streams from process-to-process over connections
Accessed via the sockets interface
Hardware & software organization of an Internet application:
Internet client host Internet server host
Client User code Server
Sockets interface
(system calls)
TCP/IP Kernel code TCP/IP
Hardware interface
(interrupts)
Network Hardware Network
adapter and firmware adapter
Global IP Internet
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 13
A Programmer’s View of the Internet
1. Hosts are mapped to a set of 32-bit IP addresses
128.2.203.179
We focus on IPv4, which uses 32-bit addresses
The new version is IPv6, which uses 128-bit addresses
2. The set of IP addresses is mapped to a set of identifiers
called Internet domain names
128.2.203.179 is mapped to www.cs.cmu.edu
3. A process on one Internet host can communicate with a
process on another Internet host over a connection
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 14
(1) IP Addresses
32-bit IP addresses are stored in an IP address struct
By convention, each byte in a 32-bit IP address is represented
by its decimal value and separated by a period
IP address: 0x8002C2F2 = 128.2.194.242
/* Internet address structure */
struct in_addr {
uint32_t s_addr; /* network byte order (big-endian) */
};
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 15
(2) Domain Naming System (DNS)
Examples of Internet domain names
The Internet maintains a mapping unnamed root
between IP addresses and domain
names in a worldwide distributed .net .edu .gov .com
database called DNS
mit cmu berkeley amazon
Conceptually, programmers can view
the DNS database as a collection of cs ece www
millions of host entries 176.32.98.166
Each host entry defines the mapping ics pdl
between a set of domain names and IP
addresses
whaleshark www
128.2.210.175 128.2.131.66
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 16
Properties of DNS Mappings
nslookup can be used to find DNS mappings
Each host has a locally defined domain name localhost
which always maps to the loopback address 127.0.0.1
linux> nslookup localhost
Address: 127.0.0.1
Use hostname to determine real domain name of local host:
linux> hostname
whaleshark.ics.cs.cmu.edu
linux> nslookup www.twitter.com
Address: 199.16.156.70
Multiple domain names could be Address: 199.16.156.102
mapped to multiple IP addresses: linux> nslookup twitter.com
Address: 199.16.156.102
Address: 199.16.156.70
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 17
(3) Internet Connections
Clients and servers communicate by sending streams of bytes
over connections. Each connection is:
Point-to-point: connects a pair of processes
Full-duplex: data can flow in both directions at the same time
Reliable: stream of bytes sent by the source is eventually received by
the destination in the same order it was sent
A socket is an endpoint of a connection
Socket address is an IPaddress:port pair
A port is a 16-bit integer that identifies a process:
Ephemeral port: Assigned automatically by client kernel when client
makes a connection request.
Well-known port: Associated with some service provided by a server
echo server: 7/echo ssh servers: 22/ssh
email server: 25/smtp Web servers: 80/http
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 18
Anatomy of a Connection
A connection is uniquely identified by the socket addresses
of its endpoints (socket pair)
(cliaddr:cliport, servaddr:servport)
Client socket address Server socket address
128.2.194.242:51213 208.216.181.15:80
Server
Client
Connection socket pair (port 80)
(128.2.194.242:51213, 208.216.181.15:80)
Client host address Server host address
128.2.194.242 208.216.181.15
51213 is an ephemeral port 80 is a well-known port
allocated by the kernel associated with Web servers
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 19
Using Ports to Identify Services
Server host 128.2.194.242
Client host Service request for Web server
128.2.194.242:80 (port 80)
(i.e., the Web server)
Client Kernel
Echo server
(port 7)
Service request for Web server
128.2.194.242:7 (port 80)
(i.e., the echo server)
Client Kernel
Echo server
(port 7)
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 20
Linux Networking Commands
hostname netstat
nslookup dig
telnet route
ifconfig host
ping arp
traceroute ethtool
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 21
Outline
Networks
Global IP Internet
The Sockets Interface
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 22
Sockets
What is a socket?
To the kernel, a socket is an endpoint of communication
To an application, a socket is a file descriptor that lets the application
read/write from/to the network
Socket interface = set of system-level functions used to build
network applications
Clients and servers communicate with each other by reading
from and writing to socket descriptors
Client Server
clientfd serverfd
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 23
2. Start client 1. Start server Sockets
Client Server
getaddrinfo getaddrinfo
Interface
socket socket
open_listenfd
open_clientfd bind
listen
Connection
request
connect accept
3. Exchange
Client / write read data
Server
Session Await connection
read write request from
next client
EOF
close read
5. Drop client
4. Disconnect client
close
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 24
Sockets
Client Server
getaddrinfo getaddrinfo
Interface
socket socket
open_listenfd
open_clientfd bind
listen
Connection
request
connect accept
Client / write read
Server
Session Await connection
read write request from
next client
EOF
close read
close
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 25
Sockets Interface
Function Parameters Usage
socket int domain, Clients & servers use the socket function
int type, to create a socket descriptor
int protocol
bind int sockfd, A server uses bind to associate the
SA *addr, server’s socket address with a socket
socklen_t addrlen descriptor
listen int sockfd, A server calls the listen function so that
int backlog a descriptor will be used by a server (for
listening) rather than a client
accept int listenfd, A server waits for connection requests
SA *addr, from clients by calling accept
int *addrlen
connect int clientfd, A client establishes a connection with a
SA *addr, server by calling connect
socklen_t addrlen
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 26
For More Information
W. Richard Stevens et. al. “Unix Network
Programming: The Sockets Networking API”,
Volume 1, Third Edition, Prentice Hall, 2003
THE network programming bible.
Michael Kerrisk, “The Linux Programming
Interface”, No Starch Press, 2010
THE Linux programming bible.
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 27