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@) Network-Application Interface
u Defines how apps use the network
u Application Layer APIs
u Lets apps talk to each other
u hides the other layers of the network app app
network
2 Computer Networks host host Project 1
u Simple Client
u Send requests to attu server
u Wait for a reply
u Extract the information from the reply u Continue… u Simple Server
u Server handles the Client requests u Multi-threaded
Computer Networks 3 Project 1
u This is the basis for many apps! u File transfer: send name, get file (§6.1.4)
u Web browsing: send URL, get page u Echo: send message, get it back u Let’s see how to write this app …
Computer Networks 4 Socket API (Generalized)
u Simple application-layer abstractions (APIs) to use the network u The network service API used to write all Internet applications u Part of all major OSes and languages; originally Berkeley (Unix) ~1983
u Tw o k i n d s o f s o c k e t s
u Streams (TCP): reliably send a stream of bytes u Datagrams (UDP): unreliably send separate messages
Computer Networks 5 Socket API (2)
u Sockets let apps attach to the local network at different ports u Ports are used by OS to distinguish services/apps using internet
Socket Socket Port 1 Port 2
Computer Networks 6 Socket API (3)
Primitive Meaning SOCKET Create a new communication endpoint BIND Associate a local address (port) with a socket Announce willingness to accept connections; (give LISTEN 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 7 https://docs.oracle.com/javase/8/docs/api/java/net/Socket.html https://docs.oracle.com/javase/8/docs/api/java/net/ServerSocket.html Using Sockets Client (host 1) Time Server (host 2)
Computer Networks 8 Using Sockets (2) Client (host 1) Time Server (host 2)
connect 1 1
request 2 reply 3 disconnect 4 4 Computer Networks 9 Using Sockets (3) Client (host 1) Time Server (host 2) 1: socket 1: socket 2: (bind) connect 3: (listen) 4: accept* 5: connect* 6: recv* request *= call blocks 7: send 8: recv* reply 9: send disconnect 10: close 10: close Computer Networks 10 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 11 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 12 Java Examples with Socket & ServerSocket u Server u Client
ServerSocket listener = new ServerSocket(9090); Socket socket = new Socket(server, 9090); try { out = while (true) { new PrintWriter(socket.getOutputStream(), true); Socket socket = listener.accept(); socket.close(); try { socket.getInputStream(); } finally { socket.close(); } • http://cs.lmu.edu/~ray/notes/javanetexampl } es/ } • https://docs.oracle.com/javase/tutorial/net finally { working/datagrams/clientServer.html listener.close(); • https://docs.oracle.com/javase/tutorial/net } working/sockets/index.html
Computer Networks 13 Traceroute
u Apps talk to other apps with no real idea of what is inside the network
u This is good! But you may be curious … u Peeking inside the Network with Traceroute
app app
??? host host Traceroute Van Jacobson u Widely used command-line tool to let hosts peek inside the network
u On all OSes (tracert on Windows) u Developed by Van Jacobson ~1987
u Uses a network-network interface (IP) in ways we will explain later
: Credit: Wikipedia (public domain)
Computer Networks 15 Traceroute u Probes successive hops to find network path u TTL: time-to-live
. . .
Local Remote ComputerHost Networks 16 Host Traceroute
. . . 1 hop 2 hops 3 hops N-1 hops N hops Local Remote Host Host
Computer Networks 17 Using Traceroute
Computer Networks 18 Using Traceroute (2)
u ISP names and places are educated guesses
Home tde Telefonica Level3 pnw-gigapop UW 1 hop 3 hops 4 hops 6 hops 1 hop 3 hops
. . .
100 ms UW 180 ms >200 ms My computer NYC San Jose Seattle www.uw.edu (www1.cac.washington.edu) Computer Networks 19 END
© 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 Computer Networks 20