computer networks - university of washington · 2019-01-20 · computer networks 10. . . my...

23
Computer Networks The Socket API (Project 1) & Traceroute (HW 1) (§1.3.4, 6.1.2-6.1.4) Originally By David Wetherall (djw@), Modified By Qian Yan (qiany7@)

Upload: others

Post on 18-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Networks - University of Washington · 2019-01-20 · Computer Networks 10. . . My computer () tde 3 hops Telefonica 4 hops Level3 6 hops pnw-gigapop 1 hop UW 3 hops NYC

Computer NetworksThe Socket API (Project 1) & Traceroute (HW 1)

(§1.3.4, 6.1.2-6.1.4)

Originally By David Wetherall (djw@), Modified By Qian Yan (qiany7@)

Page 2: Computer Networks - University of Washington · 2019-01-20 · Computer Networks 10. . . My computer () tde 3 hops Telefonica 4 hops Level3 6 hops pnw-gigapop 1 hop UW 3 hops NYC

About Me

I’m Anir! (Anirban)

o 5th year masters student

I love low level systems programming and distributed systems

This is my 2nd quarter as a TA

Interned at CDK Global, Facbeookand VMware in the past

Currently doing research in the Systems Lab

Computer Networks 2

Page 3: Computer Networks - University of Washington · 2019-01-20 · Computer Networks 10. . . My computer () tde 3 hops Telefonica 4 hops Level3 6 hops pnw-gigapop 1 hop UW 3 hops NYC

About Me

Michael (Chenyang)

Q: What do I like?

A: Scuba Diving + music

Q: What kind of music do you like?

A: British ones, like those made by

The Stone Roses

Q: What song do you listen to

recently?

A: Step into My World by Hurricane

#1Computer Networks 3

Page 4: Computer Networks - University of Washington · 2019-01-20 · Computer Networks 10. . . My computer () tde 3 hops Telefonica 4 hops Level3 6 hops pnw-gigapop 1 hop UW 3 hops NYC

Administrivia

Projects (15 + 15 + 15)%

Groups up to 3

Any language. Provide build instructions. We can only help with C/Java/Python

Homework (10%)

Textbook questions + traceroute/wireshark

Individual

Pop quiz (5%)

5 quizzes. Lowest score dropped

Midterm (15%)

Final (25%)

Computer Networks 4

Page 5: Computer Networks - University of Washington · 2019-01-20 · Computer Networks 10. . . My computer () tde 3 hops Telefonica 4 hops Level3 6 hops pnw-gigapop 1 hop UW 3 hops NYC

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

host

appapp

host

???

Page 6: Computer Networks - University of Washington · 2019-01-20 · Computer Networks 10. . . My computer () tde 3 hops Telefonica 4 hops Level3 6 hops pnw-gigapop 1 hop UW 3 hops NYC

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 6

: Credit: Wikipedia (public domain)

Van Jacobson

Page 7: Computer Networks - University of Washington · 2019-01-20 · Computer Networks 10. . . My computer () tde 3 hops Telefonica 4 hops Level3 6 hops pnw-gigapop 1 hop UW 3 hops NYC

Traceroute

Probes successive hops to find network

path

TTL: time-to-live

Computer Networks 7

. . .

Local

HostRemote

Host

Page 8: Computer Networks - University of Washington · 2019-01-20 · Computer Networks 10. . . My computer () tde 3 hops Telefonica 4 hops Level3 6 hops pnw-gigapop 1 hop UW 3 hops NYC

Traceroute

Computer Networks 8

. . .

Local

HostRemote

Host

1 hop2 hops

3 hops N-1 hopsN hops

Page 9: Computer Networks - University of Washington · 2019-01-20 · Computer Networks 10. . . My computer () tde 3 hops Telefonica 4 hops Level3 6 hops pnw-gigapop 1 hop UW 3 hops NYC

Using Traceroute

Computer Networks 9

Page 10: Computer Networks - University of Washington · 2019-01-20 · Computer Networks 10. . . My computer () tde 3 hops Telefonica 4 hops Level3 6 hops pnw-gigapop 1 hop UW 3 hops NYC

Using Traceroute (2)

ISP names and places are educated guesses

Computer Networks 10

. . .

My computer www.uw.edu

(www1.cac.washington.edu)

tde

3 hops

Telefonica

4 hops

Level3

6 hopspnw-gigapop

1 hop

UW

3 hops

NYC San Jose Seattle

UW

Home

1 hop

100 ms180 ms

>200 ms

Page 11: Computer Networks - University of Washington · 2019-01-20 · Computer Networks 10. . . My computer () tde 3 hops Telefonica 4 hops Level3 6 hops pnw-gigapop 1 hop UW 3 hops NYC

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

Computer Networks11

host

appapp

host

network

Page 12: Computer Networks - University of Washington · 2019-01-20 · Computer Networks 10. . . My computer () tde 3 hops Telefonica 4 hops Level3 6 hops pnw-gigapop 1 hop UW 3 hops NYC

Project 1

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

Computer Networks 12

Page 13: Computer Networks - University of Washington · 2019-01-20 · Computer Networks 10. . . My computer () tde 3 hops Telefonica 4 hops Level3 6 hops pnw-gigapop 1 hop UW 3 hops NYC

Project 1

This is the basis for many apps!

File transfer: send name, get file (§6.1.4)

Web browsing: send URL, get page

Echo: send message, get it back

Let’s see how to write this app …

Computer Networks 13

Page 14: Computer Networks - University of Washington · 2019-01-20 · Computer Networks 10. . . My computer () tde 3 hops Telefonica 4 hops Level3 6 hops pnw-gigapop 1 hop UW 3 hops NYC

Socket API (Generalized)

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

Datagrams (UDP): unreliably send separate messages

Computer Networks 14

Page 15: Computer Networks - University of Washington · 2019-01-20 · Computer Networks 10. . . My computer () tde 3 hops Telefonica 4 hops Level3 6 hops pnw-gigapop 1 hop UW 3 hops NYC

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

Computer Networks 15

Socket

Port 1

Socket

Port 2

Page 16: Computer Networks - University of Washington · 2019-01-20 · Computer Networks 10. . . My computer () tde 3 hops Telefonica 4 hops Level3 6 hops pnw-gigapop 1 hop UW 3 hops NYC

Socket API (3)

Computer Networks 16

Primitive Meaning

SOCKET Create a new communication endpoint

BIND Associate a local address (port) with a socket

LISTENAnnounce 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

CLOSE Release the connection

https://docs.oracle.com/javase/8/docs/api/java/net/Socket.html

https://docs.oracle.com/javase/8/docs/api/java/net/ServerSocket.html

Page 17: Computer Networks - University of Washington · 2019-01-20 · Computer Networks 10. . . My computer () tde 3 hops Telefonica 4 hops Level3 6 hops pnw-gigapop 1 hop UW 3 hops NYC

Using Sockets

Computer Networks 17

Client (host 1) Server (host 2)Time

Page 18: Computer Networks - University of Washington · 2019-01-20 · Computer Networks 10. . . My computer () tde 3 hops Telefonica 4 hops Level3 6 hops pnw-gigapop 1 hop UW 3 hops NYC

Using Sockets (2)

Computer Networks 18

Client (host 1) Server (host 2)Time

request

reply

disconnect

1 1

2

3

44

connect

Page 19: Computer Networks - University of Washington · 2019-01-20 · Computer Networks 10. . . My computer () tde 3 hops Telefonica 4 hops Level3 6 hops pnw-gigapop 1 hop UW 3 hops NYC

Using Sockets (3)

Computer Networks 19

Client (host 1) Server (host 2)Time

5: connect*

1: socket2: (bind)

1: socket

3: (listen)

9: send

6: recv*

4: accept*

7: send

8: recv*

10: close 10: close

request

reply

disconnect

connect

*= call blocks

Page 20: Computer Networks - University of Washington · 2019-01-20 · Computer Networks 10. . . My computer () tde 3 hops Telefonica 4 hops Level3 6 hops pnw-gigapop 1 hop UW 3 hops NYC

Client Program (outline)

socket() // make socket

getaddrinfo() // server and port name

// www.example.com:80

connect() // connect to server [block]

send() // send request

recv() // await reply [block]

… // do something with data!

close() // done, disconnect

Computer Networks 20

Page 21: Computer Networks - University of Washington · 2019-01-20 · Computer Networks 10. . . My computer () tde 3 hops Telefonica 4 hops Level3 6 hops pnw-gigapop 1 hop UW 3 hops NYC

Server Program (outline)

socket() // make socket

getaddrinfo() // for port on this host

bind() // associate port with socket

listen() // prepare to accept connections

accept() // wait for a connection [block]

recv() // wait for request

send() // send the reply

close() // eventually disconnect

Computer Networks 21

Page 22: Computer Networks - University of Washington · 2019-01-20 · Computer Networks 10. . . My computer () tde 3 hops Telefonica 4 hops Level3 6 hops pnw-gigapop 1 hop UW 3 hops NYC

Java Examples with Socket & ServerSocket

Server

Computer Networks 22

• http://cs.lmu.edu/~ray/notes/javanetexampl

es/

• https://docs.oracle.com/javase/tutorial/net

working/datagrams/clientServer.html

• https://docs.oracle.com/javase/tutorial/net

working/sockets/index.html

Client

ServerSocket 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();

Page 23: Computer Networks - University of Washington · 2019-01-20 · Computer Networks 10. . . My computer () tde 3 hops Telefonica 4 hops Level3 6 hops pnw-gigapop 1 hop UW 3 hops NYC

END

Computer Networks 23

© 2013 D. Wetherall

Slide material from: TANENBAUM, ANDREW S.; WETHERALL, DAVID J., COMPUTER NETWORKS, 5th

Edition, © 2011. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle

River, New Jersey