Computer Networks
The Socket API (Project 1) & Traceroute (HW 1)
Originally By David Wetherall (djw@)
Outline
Ø
Administrivia
Ø
Project 1 and Socket API
Administrivia
Computer Networks 3
Ø
Sections will be recorded
Ø
HW1 & Traceroute Experiment due tomorrow
Ø
HW2 due Jan 21
st(next Friday)
Network-Application Interface
Ø
Defines how apps use the network
Ø Application Layer APIsØ Lets apps talk to each other
Ø hides the other layers of the network
app
app
Project 1
Computer Networks 5
Ø
Simple Client
Ø Send requests to attu server Ø Wait for a reply
Ø Extract the information from the reply Ø Continue…
Ø
Simple Server
Ø Server handles the Client requests Ø Multi-threaded
Client
Time
Server
Computer Networks
request
reply
extract
information
request
Continue…
Project 1
Ø
This is the basis for many apps!
Ø File transfer: send name, get file Ø Web browsing: send URL, get page Ø Echo: send message, get it back Ø Let’s see how to write this app …Socket API (Generalized)
Computer Networks 7
Ø Simple application-layer abstractions (APIs) to use the network Ø The network service API used to write all Internet
applications
Ø Part of all major OSes and languages; originally Berkeley (Unix) ~1983
Ø Two kinds of sockets
Ø Streams (TCP): reliably send a stream of bytes
Socket API (2)
Ø
Sockets let apps attach to the local network at different ports
Ø
Ports are used by OS to distinguish services/apps using internet
Socket Port 1
Socket Port 2
Socket API (3)
Computer Networks https://docs.oracle.com/javase/8/docs/api/java/net/Socket.html https://docs.oracle.com/javase/8/docs/api/java/net/ServerSocket.html 7Primitive
Meaning
SOCKET
Create a new communication endpoint
BIND
Associate a local address (port) with a socket
LISTEN
Announce willingness to accept connections; (give
queue size)
ACCEPT
Passively establish an incoming connection
CONNECT Actively attempt to establish a connection
SEND
Send some data over the connection
RECEIVE
Receive some data from the connection
Using TCP Sockets (2)
Client (host 1)
Time
Server (host 2)
request
reply
disconnect
1
1
2
3
connect
Using TCP Sockets (3)
Client (host 1)
Time
Server (host 2)
5: connect*
1: socket
9: send
6: recv*
7: send
8: recv*
11: close
request
Computer Networks 11reply
disconnect
connect
*= call blocks
1: socket
2: (bind)
3: (listen)
4: accept*
10: recv*
12: close
Using UDP Sockets
Client (host 1)
Time
Server (host 2)
5: connect*
1: socket
9: sendto
6: recvfrom*
7: sendto
8: recvfrom*
request
reply
disconnect
connect
*= call blocks
1: socket
2: (bind)
3: (listen)
4: accept*
10: recvfrom*
Client Program (outline)
Computer Networks 13
socket()
// make socket
getaddrinfo() // server and portname
// www.example.com:80
connect() // connect to server [block]
…
send()
recv()
…
close()
// send request
// await reply [block]
// do something with data!
Server Program (outline)
socket() // make socket
getaddrinfo() // for port on this host // associate port with socket // prepare to accept connections // wait for a connection [block] bind() listen() accept() … recv() … send() close()
// wait for request [block] // send the reply
Java Examples with Socket & ServerSocket
Computer Networks 15Ø
Server
• http://cs.lmu.edu/~ray/notes/javanetexamples/ • https://docs.oracle.com/javase/tutorial/net working/datagrams/clientServer.html • https://docs.oracle.com/javase/tutorial/net working/sockets/index.htmlServerSocket listener = new ServerSocket(9090);
try{
while (true) {
Socket socket = listener.accept();
try{ socket.getInputStream(); } finally { socket.close(); } } } finally { listener.close(); }
Socket socket = new Socket(server,9090);
out=
new PrintWriter(socket.getOutputStream(), true); socket.close();
Traceroute
Ø
Apps talk to other apps with no real idea of what is inside
the network
Ø This is good! But you may be curious …
Ø
Peeking inside the Network with Traceroute
app
app
Traceroute
Ø Widely used command-line tool to let hosts peek inside the network
Ø On all OSes (tracert on Windows) Ø Developed by Van Jacobson ~1987
Ø Uses a network-network interface (IP) in ways we will explain later
Computer Networks 15
: Credit: Wikipedia (public domain)
Traceroute
Ø
Probes successive hops to find network path
Ø
Core mechanism: Time-To-Live(TTL)
Ø
TTL == 0?
Discard data, error (ICMP) report to sender
Continue with TTL-1
Local
Traceroute
. . .
Local
Host
1 hop
2 hops
3 hops
N-1 hops
N hops
Remote
Host
Computer Networks 19Using
Traceroute
21
Using Traceroute
Router settings
affect results
Using Traceroute (2)
Ø
ISP names and places are educatedguesses
My computer
www.uw.edu
(www1.cac.washington.edu)
Level3 pnw-gigapop
UW
6 hops
1 hop
3 hops
. . .
tde Telefonica
3 hops
4 hops
100 ms
180 ms
NYC
San Jose
Computer Networks 23
UW
>200 ms
Seattle
Home
1 hop
END
© 2013 D. Wetherall