computer networks - university of washington · computer networks 3 about me i’m john! senior in...

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 Bradford Chen (bradchen@)

Upload: others

Post on 10-Sep-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Networks - University of Washington · Computer Networks 3 About Me I’m John! Senior in CSE I too love distributed systems, OS, and network programming This is my 2nd quarter

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 Bradford Chen (bradchen@)

Page 2: Computer Networks - University of Washington · Computer Networks 3 About Me I’m John! Senior in CSE I too love distributed systems, OS, and network programming This is my 2nd quarter

About Me

Computer Networks 2

I’m Brad!

Senior in CSE

I love distributed systems and network programming

This is my 2nd quarter as a TA

Interned at Amazon and Snowflake in the past

Page 3: Computer Networks - University of Washington · Computer Networks 3 About Me I’m John! Senior in CSE I too love distributed systems, OS, and network programming This is my 2nd quarter

Computer Networks 3

About Me

Computer Networks 3

I’m John!

Senior in CSE

I too love distributed systems, OS, and network programming

This is my 2nd quarter as a TA

Planning to join AWS in August

Page 4: Computer Networks - University of Washington · Computer Networks 3 About Me I’m John! Senior in CSE I too love distributed systems, OS, and network programming This is my 2nd quarter

Network-Application Interface

Defines how apps use the network

Application LayerAPIs

Lets apps talk to each other

hides the other layers of the network

11Computer Networks

host

appapp

host

network

Page 5: Computer Networks - University of Washington · Computer Networks 3 About Me I’m John! Senior in CSE I too love distributed systems, OS, and network programming This is my 2nd quarter

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

Page 6: Computer Networks - University of Washington · Computer Networks 3 About Me I’m John! Senior in CSE I too love distributed systems, OS, and network programming This is my 2nd quarter

Project 1

Computer Networks 6

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 …

Page 7: Computer Networks - University of Washington · Computer Networks 3 About Me I’m John! Senior in CSE I too love distributed systems, OS, and network programming This is my 2nd quarter

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

Datagrams (UDP): unreliably send separate messages

Page 8: Computer Networks - University of Washington · Computer Networks 3 About Me I’m John! Senior in CSE I too love distributed systems, OS, and network programming This is my 2nd quarter

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

Computer Networks 8

Page 9: Computer Networks - University of Washington · Computer Networks 3 About Me I’m John! Senior in CSE I too love distributed systems, OS, and network programming This is my 2nd quarter

Socket API (3)

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

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

Page 10: Computer Networks - University of Washington · Computer Networks 3 About Me I’m John! Senior in CSE I too love distributed systems, OS, and network programming This is my 2nd quarter

Using Sockets

Client (host 1) Time Server (host 2)

Computer Networks 10

Page 11: Computer Networks - University of Washington · Computer Networks 3 About Me I’m John! Senior in CSE I too love distributed systems, OS, and network programming This is my 2nd quarter

Using Sockets (2)

Client (host 1) Time Server (host 2)

request

Computer Networks 11

reply

1 1

2

3

disconnect44

connect

Page 12: Computer Networks - University of Washington · Computer Networks 3 About Me I’m John! Senior in CSE I too love distributed systems, OS, and network programming This is my 2nd quarter

Using Sockets (3)

Client (host 1) Time Server (host 2)

5: connect*

1: socket

6: recv*

1: socket

2: (bind)3: (listen)

4: accept*

7: send

8: recv*

10: close

9: send

10: close

request

Computer Networks 12

reply

disconnect

connect

*= call blocks

Page 13: Computer Networks - University of Washington · Computer Networks 3 About Me I’m John! Senior in CSE I too love distributed systems, OS, and network programming This is my 2nd quarter

Client Program (outline)

Computer Networks 13

socket() // make socket

getaddrinfo() // server and port name

// www.example.com:80

connect() // connect to server [block]

send()

recv()

close()

// send request

// await reply [block]

// do something with data!

// done, disconnect

Page 14: Computer Networks - University of Washington · Computer Networks 3 About Me I’m John! Senior in CSE I too love distributed systems, OS, and network programming This is my 2nd quarter

Server Program (outline)

Computer Networks 14

socket() // make socket

getaddrinfo() // for port on this host

// associate port with socket

// prepare to accept connections

// wait for a connection [block]

// wait for request

bind()

listen()

accept()

recv()

send()

close()

// send the reply

// eventually disconnect

Page 15: Computer Networks - University of Washington · Computer Networks 3 About Me I’m John! Senior in CSE I too love distributed systems, OS, and network programming This is my 2nd quarter

Java Examples with Socket & ServerSocket

Computer Networks 15

• 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

Server 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 16: Computer Networks - University of Washington · Computer Networks 3 About Me I’m John! Senior in CSE I too love distributed systems, OS, and network programming This is my 2nd quarter

Questions?

Page 17: Computer Networks - University of Washington · Computer Networks 3 About Me I’m John! Senior in CSE I too love distributed systems, OS, and network programming This is my 2nd quarter

Traceroute

host

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

host

???

Page 18: Computer Networks - University of Washington · Computer Networks 3 About Me I’m John! Senior in CSE I too love distributed systems, OS, and network programming This is my 2nd quarter

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 19: Computer Networks - University of Washington · Computer Networks 3 About Me I’m John! Senior in CSE I too love distributed systems, OS, and network programming This is my 2nd quarter

Traceroute

Probes successive hops to find network

path

TTL: time-to-live

7

. . .

LocalCHompoutesr Ntetworks

Remote

Host

Page 20: Computer Networks - University of Washington · Computer Networks 3 About Me I’m John! Senior in CSE I too love distributed systems, OS, and network programming This is my 2nd quarter

Traceroute

. . .

Local

Host

1 hop2 hops

3 hops N-1 hopsN hops

Remote

Host

Computer Networks 20

Page 21: Computer Networks - University of Washington · Computer Networks 3 About Me I’m John! Senior in CSE I too love distributed systems, OS, and network programming This is my 2nd quarter

Using Traceroute

Computer Networks 21

Page 22: Computer Networks - University of Washington · Computer Networks 3 About Me I’m John! Senior in CSE I too love distributed systems, OS, and network programming This is my 2nd quarter

Using Traceroute (2)

ISP names and places are educatedguesses

My computer

tde Telefonica

3 hops 4 hops

Level3 pnw-gigapop UW 6 hops 1 hop 3 hops

Home

1 hop

. . .

100 ms180 ms

NYC San Jose

Computer Networks 22

UW

>200 ms

Seattle www.uw.edu

(www1.cac.washington.edu)

Page 23: Computer Networks - University of Washington · Computer Networks 3 About Me I’m John! Senior in CSE I too love distributed systems, OS, and network programming This is my 2nd quarter

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