Presentation is loading. Please wait.

Presentation is loading. Please wait.

Network Applications: Network App Programming: TCP FTP Y. Richard Yang 2/8/2016.

Similar presentations


Presentation on theme: "Network Applications: Network App Programming: TCP FTP Y. Richard Yang 2/8/2016."— Presentation transcript:

1 Network Applications: Network App Programming: TCP FTP Y. Richard Yang http://zoo.cs.yale.edu/classes/cs433/ 2/8/2016

2 2 Outline  Admin and recap r Network app programming r FTP

3 3 Recap: DNS Protocol, Messages Many features: typically over UDP (can use TCP); query and reply messages with the same message format; length/content encoding of names; simple compression; additional info as server push https://www.ietf.org/rfc/rfc1035.txt

4 Recap: Connectionless UDP: Big Picture (Java version) close clientSocket Server (running on serv ) read reply from clientSocket create socket, clientSocket = DatagramSocket() Client Create datagram using ( serv, x) as (dest addr. port), send request using clientSocket create socket, port= x, for incoming request: serverSocket = DatagramSocket( x ) read request from serverSocket write reply to serverSocket generate reply, create datagram using client host address, port number

5 5 Recap: UDP Sockets server UDP socket space Public address: 128.36.59.2 Local address: 127.0.0.1 address: {128.36.232.5:53} snd/recv buf: InetAddress sIP1 = InetAddress.getByName(“localhost”); DatagramSocket ssock1 = new DatagramSocket(9876, sIP1); InetAddress sIP2 = InetAddress.getByName(“128.36.59.2”); DatagramSocket ssock2 = new DatagramSocket(9876,sIP2); DatagramSocket serverSocket = new DatagramSocket(6789); address: {127.0.0.1:9876} snd/recv buf: address: {128.36.59.2:9876} snd/recv buf: address: {*:6789} snd/recv buf: UDP demutiplexing is based on matching (dst address, dst port)

6 Recap: Data Encoding/Decoding r Pay attention to encoding/decoding of data: transport layer handles only a sequence of bytes 6 byte array query encoding decoding client server if not careful, query sent != query received result

7 Discussion: UDP/DNS Server Pseudocode 7 r Modify the example UDP server code to implement a local DNS server. requesting host cyndra.cs.yale.edu gaia.cs.umass.edu root name server 1 2 3 4 authoritative name server dns.cs.umass.edu 5 6 TLD name server 7 8 local name server 130.132.1.9

8 8 UDP/DNS Implementation r Standard UDP demultiplexing (find out return address by src.addr/src.port of UDP packet) does not always work r DNS solution: identification: remember the mapping

9 Outline r Recap r Network application programming m Overview m UDP  Basic TCP 9

10 TCP Socket Design: Starting w/ UDP server Socket socket space address: {*:9876} snd/recv buf: 128.36.232.5 128.36.230.2 local address local port Issue: TCP is designed to provide a pipe abstraction: server reads an ordered sequence of bytes from each individual connected client Issue 2: How is the server notified that a new client is connected? P1 client1 P2 client2 sock.nextByte(client1)? newClient = sock.getNewClient()?

11 BSD TCP Socket API Design server TCP socket space 128.36.232.5 128.36.230.2 Q: How to decide where to put a new packet? address: {*:9876} snd/recv buf: address: {*:9876; client 1 IP/port} snd/recv buf: address: {*:9876; client 2 IP/port} snd/recv buf: socket for new connected clients P1 client1 P2 client2 A: Packet demutiplexing is based on four tuples: (dst addr, dst port, src addr, src port) An individual socket for client 1 An individual socket for client 2

12 TCP Connection-Oriented Demux r TCP socket identified by 4-tuple: m source IP address m source port number m dest IP address m dest port number r recv host uses all four values to direct segment to appropriate socket m different connections/sessions are automatically separated into different sockets

13 13 TCP Socket Big Picture -Welcome socket: the waiting room -connSocket: the operation room

14 Client/server Socket Workflow: TCP wait for incoming connection request connectionSocket = welcomeSocket.accept() create socket, port= x, for incoming request: welcomeSocket = ServerSocket(x) create socket, connect to hostid, port= x clientSocket = Socket() close connectionSocket read reply from clientSocket close clientSocket Server (running on hostid ) Client send request using clientSocket read request from connectionSocket write reply to connectionSocket TCP connection setup

15 Server Flow connSocket = accept() Create ServerSocket(6789) read request from connSocket Serve the request close connSocket -Welcome socket: the waiting room -connSocket: the operation room

16 16 ServerSocket r ServerSocket() m creates an unbound server socket. r ServerSocket(int port) m creates a server socket, bound to the specified port. r ServerSocket(int port, int backlog) m creates a server socket and binds it to the specified local port number, with the specified backlog. r ServerSocket(int port, int backlog, InetAddress bindAddr) m creates a server with the specified port, listen backlog, and local IP address to bind to. r bind(SocketAddress endpoint) m binds the ServerSocket to a specific address (IP address and port number). r bind(SocketAddress endpoint, int backlog) m binds the ServerSocket to a specific address (IP address and port number). r Socket accept() m listens for a connection to be made to this socket and accepts it. r close() closes this socket.

17 17 (Client)Socket r Socket(InetAddress address, int port) creates a stream socket and connects it to the specified port number at the specified IP address. r Socket(InetAddress address, int port, InetAddress localAddr, int localPort) creates a socket and connects it to the specified remote address on the specified remote port. r Socket(String host, int port) creates a stream socket and connects it to the specified port number on the named host. r bind(SocketAddress bindpoint) binds the socket to a local address. r connect(SocketAddress endpoint) connects this socket to the server. r connect(SocketAddress endpoint, int timeout) connects this socket to the server with a specified timeout value. r InputStreamget InputStream() returns an input stream for this socket. r OutputStreamgetOutputStream() returns an output stream for this socket. r close() closes this socket.

18 18 Simple TCP Example Example client-server app: 1) client reads line from standard input ( inFromUser stream), sends to server via socket ( outToServer stream) 2) server reads line from socket 3) server converts line to uppercase, sends back to client 4) client reads, prints modified line from socket ( inFromServer stream)

19 Example: Java client (TCP) import java.io.*; import java.net.*; class TCPClient { public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence; BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); sentence = inFromUser.readLine(); Socket clientSocket = new Socket(“server.name", 6789); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); Create input stream Create client socket, connect to server Create output stream attached to socket

20 OutputStream r public abstract class OutputStream m public abstract void write(int b) throws IOException m public void write(byte[] data) throws IOException m public void write(byte[] data, int offset, int length) throws IOException m public void flush( ) throws IOException m public void close( ) throws IOException 20

21 InputStream r public abstract class InputStream m public abstract int read( ) throws IOException m public int read(byte[] input) throws IOException m public int read(byte[] input, int offset, int length) throws IOException m public long skip(long n) throws IOException m public int available( ) throws IOException m public void close( ) throws IOException 21

22 Example: Java client (TCP), cont. outToServer.writeBytes(sentence + '\n'); BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); modifiedSentence = inFromServer.readLine(); System.out.println ("FROM SERVER: " + modifiedSentence ); clientSocket.close(); } Read line from server Create input stream attached to socket Send line to server

23 Example: Java server (TCP) import java.io.*; import java.net.*; class TCPServer { public static void main(String argv[]) throws Exception { String clientSentence; String capitalizedSentence; ServerSocket welcomeSocket = new ServerSocket(6789); while(true) { Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); Create welcoming socket at port 6789

24 Demo % wireshark to capture our TCP traffic tcp.srcport==6789 or tcp.dstport==6789 24

25 Under the Hood: After Welcome (Server) Socket server client TCP socket space state: listening address: {*:6789, *:*} completed connection queue: sendbuf: recvbuf: 128.36.232.5 128.36.230.2 TCP socket space state: listening address: {*:25, *:*} completed connection queue: sendbuf: recvbuf: 198.69.10.10 state: listening address: {*:25, *:*} completed connection queue: sendbuf: recvbuf: state: starting address: {198.69.10.10:1500, *:*} sendbuf: recvbuf: local addr local port remote addr remote port %netstat –p tcp –n -a

26 Client Initiates Connection server client TCP socket space state: listening address: {*:6789, *.*} completed connection queue: sendbuf: recvbuf: 128.36.232.5 128.36.230.2 TCP socket space state: listening address: {*.25, *.*} completed connection queue: sendbuf: recvbuf: 198.69.10.10 state: listening address: {*.25, *.*} completed connection queue: sendbuf: recvbuf: state: connecting address: {198.69.10.10:1500, 128.36.232.5:6789} sendbuf: recvbuf:

27 Example: Client Connection Handshake Done server client TCP socket space state: listening address: {*:6789, *:*} completed connection queue: {128.36.232.5.6789, 198.69.10.10.1500} sendbuf: recvbuf: 128.36.232.5 128.36.230.2 TCP socket space state: listening address: {*:25, *:*} completed connection queue: sendbuf: recvbuf: 198.69.10.10 state: listening address: {*:25, *:*} completed connection queue: sendbuf: recvbuf: state: connected address: {198.69.10.10:1500, 128.36.232.5:6789} sendbuf: recvbuf:

28 Example: Client Connection Handshake Done server client TCP socket space state: listening address: {*.6789, *:*} completed connection queue: sendbuf: recvbuf: 128.36.232.5 128.36.230.2 TCP socket space state: listening address: {*.25, *:*} completed connection queue: sendbuf: recvbuf: 198.69.10.10 state: listening address: {*.25, *:*} completed connection queue: sendbuf: recvbuf: state: connected address: {198.69.10.10.1500, 128.36.232.5:6789} sendbuf: recvbuf: state: established address: {128.36.232.5:6789, 198.69.10.10.1500} sendbuf: recvbuf: Packet sent to the socket with the best match! Packet demutiplexing is based on (dst addr, dst port, src addr, src port)

29 Demo r What if more client connections than backlog allowed? 29

30 Example: Java server (TCP) import java.io.*; import java.net.*; class TCPServer { public static void main(String argv[]) throws Exception { String clientSentence; String capitalizedSentence; ServerSocket welcomeSocket = new ServerSocket(6789); while(true) { Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); Wait, on welcoming socket for contact by client

31 Example: Server accept() server client TCP socket space state: listening address: {*.6789, *:*} completed connection queue: sendbuf: recvbuf: 128.36.232.5 128.36.230.2 TCP socket space state: listening address: {*.25, *:*} completed connection queue: sendbuf: recvbuf: 198.69.10.10 state: listening address: {*.25, *:*} completed connection queue: sendbuf: recvbuf: state: connected address: {198.69.10.10.1500, 128.36.232.5:6789} sendbuf: recvbuf: state: established address: {128.36.232.5:6789, 198.69.10.10.1500} sendbuf: recvbuf: connectionSocket

32 Example: Java server (TCP): Processing BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + '\n'; DataOutputStream outToClient = new DataOutputStream (connectionSocket.getOutputStream()); outToClient.writeBytes(capitalizedSentence); } Read in line from socket Create input stream, attached to socket

33 Example: Java server (TCP): Output BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + '\n'; DataOutputStream outToClient = new DataOutputStream (connectionSocket.getOutputStream()); outToClient.writeBytes(capitalizedSentence); } Create output stream, attached to socket Write out line to socket End of while loop, loop back and wait for another client connection

34 Analysis r Assume that client requests arrive at a rate of lambda/second r Assume that each request takes 1/mu seconds r Some basic questions m How big is the backlog (welcome queue) 34 Welcome Socket Queue

35 Analysis r Is there any interop issue in the sample program? 35

36 Analysis r Is there any interop issue in the sample program? m DataOutputStream writeBytes(String) truncates –http://docs.oracle.com/javase/1.4.2/docs/api/java/io/DataOu tputStream.html#writeBytes(java.lang.String) 36

37 Outline r Recap r Network application programming r FTP 37

38 38 FTP: the File Transfer Protocol r Transfer files to/from remote host r Client/server model m client: side that initiates transfer (either to/from remote) m server: remote host r ftp: RFC 959 r ftp server: port 21/20 (smtp 25, http 80) file transfer FTP server FTP user interface FTP client local file system remote file system user at host

39 39 FTP Commands, Responses Sample commands: r sent as ASCII text over control channel  USER username r PASS password  PWD returns current dir  STAT shows server status  LIST returns list of file in current directory  RETR filename retrieves (gets) file  STOR filename stores file Sample return codes r status code and phrase r 331 Username OK, password required r 125 data connection already open; transfer starting r 425 Can’t open data connection r 452 Error writing file

40 40 FTP Protocol Design r What is the simplest design of data transfer? FTP client FTP server TCP control connection port 21 at server RETR file.dat data

41 41 FTP: A Client-Server Application with Separate Control, Data Connections r Two types of TCP connections opened: m A control connection: exchange commands, responses between client, server. “out of band control” m Data connections: each for file data to/from server Discussion: why does FTP separate control/data connections? Q: How to create a new data connection?

42 42 Traditional FTP: Client Specifies Port for Data Connection FTP client FTP server TCP control connection port 21 at server PORT clientip:cport RETR file.dat Server initiates TCP data connection server:20 clientip:cport

43 43 Example using telnet/nc r Use telnet for the control channel m telnet ftp.freebsd.org 21 m user, pass m port 172,27,5,145,4,1 m list r use nc (NetCat) to receive/send data with server m nc –v –l 1025

44 44 Problem of the Client PORT Approach r Many Internet hosts are behind NAT/firewalls that block connections initiated from outside FTP client FTP server TCP control connection port 21 at server PORT clientip:cport RETR file.dat Server initiates TCP data connection server:20 clientip:cport

45 45 FTP PASV: Server Specifies Data Port, Client Initiates Connection FTP client FTP server TCP control connection port 21 at server Server initiates TCP data connection server:20 clientip:cport PORT clientip:cport RETR file.dat FTP client FTP server TCP control connection port 21 at server Client initiates TCP data connection of PASV returned serverip:sport PASV RETR file.dat serverip:sport

46 46 Example r Use Wireshark to capture traffic m Using chrome to visit ftp://ftp.freebsd.org/pub/FreeBSD/README.TXT

47 47 FTP Evaluation application transport network data link physical application transport network data link physical request reply Key questions to ask about a C-S application - Is the application extensible? - Is the application scalable? - How does the application handle server failures (being robust)? - How does the application provide security? What are some interesting design features of the FTP protocol?

48 48 FTP Extensions r FTP extensions are still being used extensively in large data set transfers (e.g., LHC) r See GridFTP to FTP extensions m https://www.ogf.org/documents/GFD.20.pdf

49 Outline r Recap r Network application programming r FTP r HTTP 49

50 Backup Slides 50

51 DataStream 51

52 Demo % wireshark to capture traffic tcp.srcport==6789 or tcp.dstport==6789 52


Download ppt "Network Applications: Network App Programming: TCP FTP Y. Richard Yang 2/8/2016."

Similar presentations


Ads by Google