Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Application Layer EEE3080F Communication Network and System Fundamentals http://web.uct.ac.za/depts/commnetwork/eee3080 H Anthony Chan; Yang Li [email protected]; [email protected] http://web.uct.ac.za/depts/commnetwork/achan.html Department of Electrical Engineering University of Cape Town What I have is only borrowed from God so that I may serve others. H Anthony Chan We can do no great things; only small things with great love. (Mother Teresa of Calcutta) Communication Networks 218 Page 1 February 23, 2007 ♦ Our goals: ♦ conceptual, implementation aspects of network application protocols ¾ client-server paradigm ¾ service models ♦ learn about protocols by examining popular application-level protocols What I have is only borrowed from God so that I may serve others. H Anthony Chan Application layer We can do no great things; only small things with great love. (Mother Teresa of Calcutta) We can do no great things; only small things with great love. (Mother Teresa of Calcutta) Communication Networks 218 Page 2 February 23, 2007 Socket ♦ Principles of network applications ♦ Web and HTTP ♦ FTP ♦ Electronic Mail: SMTP, POP3, IMAP ♦ Ssh ♦ DNS ♦ Port numbers ♦ P2P file sharing ♦ Socket programming with TCP ♦ Socket programming with UDP What I have is only borrowed from God so that I may serve others. H Anthony Chan ♦ More chapter goals ♦ specific protocols: ¾ http ¾ ftp ¾ smtp ¾ pop ¾ dns ♦ programming network applications ¾ socket API ♦ a host-local, application-created/owned, OScontrolled interface (a “door”) ♦ into which application process can both send and receive messages ♦ to/from another (remote or local) application process Communication Networks 218 Page 3 February 23, 2007 What I have is only borrowed from God so that I may serve others. H Anthony Chan We can do no great things; only small things with great love. (Mother Teresa of Calcutta) Communication Networks 218 Page 4 February 23, 2007 Socket programming Socket-programming using TCP Goal: learn how to build client/server application that communicate using sockets Socket API ♦ introduced in BSD4.1 UNIX, 1981 ♦ explicitly created, used, released by apps ♦ client/server paradigm ♦ two types of transport service via socket API: ¾unreliable datagram ¾reliable, byte stream-oriented ♦ Socket: a door between application process and end-end-transport protocol (UCP or TCP) ♦ TCP service: reliable transfer of bytes from one process to another controlled by application developer controlled by operating system process process socket TCP with buffers, variables socket TCP with buffers, variables host or server What I have is only borrowed from God so that I may serve others. H Anthony Chan We can do no great things; only small things with great love. (Mother Teresa of Calcutta) Communication Networks 218 Page 5 February 23, 2007 Socket programming with TCP We can do no great things; only small things with great love. (Mother Teresa of Calcutta) controlled by operating system host or server We can do no great things; only small things with great love. (Mother Teresa of Calcutta) Communication Networks 218 Page 6 February 23, 2007 Socket programming with TCP Client must contact server ♦ server process must first be running ♦ server must have created socket (door) that welcomes client’s contact Client contacts server by: ♦ creating client-local TCP socket ♦ specifying IP address, port number of server process What I have is only borrowed from God so that I may serve others. H Anthony Chan What I have is only borrowed from God so that I may serve others. H Anthony Chan internet controlled by application developer Communication Networks 218 Page 7 February 23, 2007 ♦ When client creates socket: client TCP establishes connection to server TCP ♦ When contacted by client, server TCP creates new socket for server process to communicate with client ¾allows server to talk with multiple clients ♦ application viewpoint ♦ TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server What I have is only borrowed from God so that I may serve others. H Anthony Chan We can do no great things; only small things with great love. (Mother Teresa of Calcutta) Communication Networks 218 Page 8 February 23, 2007 Socket programming with TCP Example client-server app Socket programming with TCP Output stream to network What I have is only borrowed from God so that I may serve others. H Anthony Chan outToServer output stream: sequence of bytes out of process Client Process inFromServer Input stream monitor inFromUser keyboard Input stream: sequence of bytes into process Input stream ♦ client reads line from standard input (inFromUser stream) , sends to server via socket (outToServer stream) ♦ server reads line from socket ♦ server converts line to uppercase, sends back to client ♦ client reads, prints modified line from socket (inFromServer stream) clientSocket TCP socket from network We can do no great things; only small things with great love. (Mother Teresa of Calcutta) Communication Networks 218 Page 9 February 23, 2007 What I have is only borrowed from God so that I may serve others. H Anthony Chan Client/server socket interaction: TCP Server (running on hostid) Client wait for incoming connection request connectionSocket = welcomeSocket.accept() write reply to connectionSocket read reply from clientSocket close connectionSocket What I have is only borrowed from God so that I may serve others. H Anthony Chan close clientSocket We can do no great things; only small things with great love. (Mother Teresa of Calcutta) Example: Java client (TCP) public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence; create socket, connect to hostid, port=x clientSocket = Socket() send request using clientSocket read request from connectionSocket Communication Networks 218 Page 10 February 23, 2007 import java.io.*; import java.net.*; class TCPClient { create socket, port=x, for incoming request: welcomeSocket = ServerSocket() TCP connection setup We can do no great things; only small things with great love. (Mother Teresa of Calcutta) Communication Networks 218 Page 11 February 23, 2007 Create input stream Create client socket, connect to server Create output stream attached to socket BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); Socket clientSocket = new Socket("hostname", 6789); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); What I have is only borrowed from God so that I may serve others. H Anthony Chan We can do no great things; only small things with great love. (Mother Teresa of Calcutta) Communication Networks 218 Page 12 February 23, 2007 Example: Java client (TCP), cont. Create BufferedReader inFromServer = input stream new BufferedReader(new attached to socket InputStreamReader(clientSocket.getInputStream())); outToServer.writeBytes(sentence + '\n'); Read line from server import java.io.*; import java.net.*; class TCPServer { public static void main(String argv[]) throws Exception { String clientSentence; String capitalizedSentence; sentence = inFromUser.readLine(); Send line to server Example: Java server (TCP) modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER: " + modifiedSentence); Create welcoming socket ServerSocket welcomeSocket = new ServerSocket(6789); at port 6789 while(true) { Wait, on welcoming socket for contact Socket connectionSocket = welcomeSocket.accept(); by client Create input BufferedReader inFromClient = stream, attached new BufferedReader(new to socket clientSocket.close(); } } InputStreamReader(connectionSocket.getInputStream())); What I have is only borrowed from God so that I may serve others. H Anthony Chan We can do no great things; only small things with great love. (Mother Teresa of Calcutta) Communication Networks 218 Page 13 February 23, 2007 What I have is only borrowed from God so that I may serve others. H Anthony Chan Example: Java server (TCP), cont Create output stream, attached DataOutputStream outToClient = to socket new DataOutputStream(connectionSocket.getOutputStream()); Read in line from socket clientSentence = inFromClient.readLine(); Write out line to socket capitalizedSentence = clientSentence.toUpperCase() + '\n'; outToClient.writeBytes(capitalizedSentence); } } } What I have is only borrowed from God so that I may serve others. H Anthony Chan End of while loop, loop back and wait for another client connection We can do no great things; only small things with great love. (Mother Teresa of Calcutta) Communication Networks 218 Page 15 February 23, 2007 We can do no great things; only small things with great love. (Mother Teresa of Calcutta) Communication Networks 218 Page 14 February 23, 2007 Application layer ♦ Principles of network applications ♦ Web and HTTP ♦ FTP ♦ Electronic Mail: SMTP, POP3, IMAP ♦ Ssh ♦ DNS ♦ Port numbers ♦ P2P file sharing ♦ Socket programming with TCP ♦ Socket programming with UDP What I have is only borrowed from God so that I may serve others. H Anthony Chan We can do no great things; only small things with great love. (Mother Teresa of Calcutta) Communication Networks 218 Page 16 February 23, 2007 Socket programming with UDP UDP: no “connection” between client and server ♦ no handshaking ♦ sender explicitly attaches IP address and port of destination ♦ server must extract IP address, port of sender from received datagram UDP: transmitted data may be received out of order, or lost application viewpoint ♦ UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server What I have is only borrowed from God so that I may serve others. H Anthony Chan We can do no great things; only small things with great love. (Mother Teresa of Calcutta) Communication Networks 218 Page 17 February 23, 2007 Example: Java client (UDP) Client Process UDP packet UDP packet create socket, clientSocket = DatagramSocket() serverSocket = DatagramSocket() Create, address (hostid, port=x, send datagram request using clientSocket read request from serverSocket write reply to serverSocket specifying client host address, port number What I have is only borrowed from God so that I may serve others. H Anthony Chan read reply from clientSocket close clientSocket We can do no great things; only small things with great love. (Mother Teresa of Calcutta) Communication Networks 218 Page 18 February 23, 2007 Example: Java client (UDP) Input: receives packet (TCP received “byte stream”) Create input stream class UDPClient { public static void main(String args[]) throws Exception { Create client socket Translate hostname to IP address using DNS Client UDP Socket to network What I have is only borrowed from God so that I may serve others. H Anthony Chan create socket, port=x, for incoming request: import java.io.*; import java.net.*; receivePacket Output: sends packet (TCP sent “byte stream”) sendPacket Input stream Client Server (running on hostid) monitor inFromUser keyboard Client/server socket interaction: UDP We can do no great things; only small things with great love. (Mother Teresa of Calcutta) BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName("hostname"); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; String sentence = inFromUser.readLine(); from network Communication Networks 218 Page 19 February 23, 2007 sendData = sentence.getBytes(); What I have is only borrowed from God so that I may serve others. H Anthony Chan We can do no great things; only small things with great love. (Mother Teresa of Calcutta) Communication Networks 218 Page 20 February 23, 2007 Example: Java client (UDP), cont. Create datagram with data-to-send, length, IP addr, port Example: Java server (UDP) import java.io.*; import java.net.*; DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); Send datagram to server clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); Read datagram from server Create datagram socket at port 9876 clientSocket.receive(receivePacket); while(true) { System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); } } Create space for received datagram Receive datagram We can do no great things; only small things with great love. (Mother Teresa of Calcutta) Communication Networks 218 Page 21 February 23, 2007 Example: Java server (UDP), cont String sentence = new String(receivePacket.getData()); Get IP addr port #, of sender DatagramSocket serverSocket = new DatagramSocket(9876); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; String modifiedSentence = new String(receivePacket.getData()); What I have is only borrowed from God so that I may serve others. H Anthony Chan class UDPServer { public static void main(String args[]) throws Exception { InetAddress IPAddress = receivePacket.getAddress(); What I have is only borrowed from God so that I may serve others. H Anthony Chan DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); We can do no great things; only small things with great love. (Mother Teresa of Calcutta) Communication Networks 218 Page 22 February 23, 2007 Socket programming: references C-language tutorial (audio/slides): ♦ “Unix Network Programming” (J. Kurose), ♦ http://manic.cs.umass.edu/~amldemo/courseware/intro. int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase(); sendData = capitalizedSentence.getBytes(); Create datagram to send to client DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); Write out datagram to socket serverSocket.send(sendPacket); } } } What I have is only borrowed from God so that I may serve others. H Anthony Chan Java-tutorials: ♦ “All About Sockets” (Sun tutorial), http://www.javaworld.com/javaworld/jw-12-1996/jw-12sockets.html ♦ “Socket Programming in Java: a tutorial,” http://www.javaworld.com/javaworld/jw-12-1996/jw-12sockets.html End of while loop, loop back and wait for another datagram We can do no great things; only small things with great love. (Mother Teresa of Calcutta) Communication Networks 218 Page 23 February 23, 2007 What I have is only borrowed from God so that I may serve others. H Anthony Chan We can do no great things; only small things with great love. (Mother Teresa of Calcutta) Communication Networks 218 Page 24 February 23, 2007 Application Layer: Summary Our study of network apps now complete! ♦ application service requirements: ¾ reliability, bandwidth, delay ♦ client-server paradigm ♦ Internet transport service model ¾ connection-oriented, reliable: TCP ¾ unreliable, datagrams: UDP What I have is only borrowed from God so that I may serve others. H Anthony Chan ♦ specific protocols: ¾ http ¾ ftp ¾ smtp, pop3 ¾ ssh ¾ dns ♦ socket programming ¾ client/server implementation ¾ using tcp, udp sockets We can do no great things; only small things with great love. (Mother Teresa of Calcutta) Communication Networks 218 Page 25 February 23, 2007 EEE3080F Communication Network and System Fundamentals We can do no great things; only small things with great love. (Mother Teresa of Calcutta) © 2003-2007 What I have is only borrowed from God so that I may serve others. H Anthony Chan We can do no great things; only small things with great love. (Mother Teresa of Calcutta) Communication Networks 218 Page 27 February 23, 2007 Application Layer: Summary Most importantly: learned about protocols ♦ typical request/reply message exchange: ¾ client requests info or service ¾ server responds with data, status code ♦ message formats: ¾ headers: fields giving info about data ¾ data: info being communicated What I have is only borrowed from God so that I may serve others. H Anthony Chan ♦ control vs. data msgs ¾ in-band, out-of-band ♦ centralized vs. decentralized ♦ stateless vs. stateful ♦ reliable vs. unreliable msg transfer ♦ “complexity at network edge” ♦ security: authentication We can do no great things; only small things with great love. (Mother Teresa of Calcutta) Communication Networks 218 Page 26 February 23, 2007