Cours du 19 octobre - PowerPoint PPT Presentation

cours du 19 octobre n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Cours du 19 octobre PowerPoint Presentation
Download Presentation
Cours du 19 octobre

play fullscreen
1 / 161
Cours du 19 octobre
116 Views
Download Presentation
halima
Download Presentation

Cours du 19 octobre

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Cours du 19 octobre Couche application Sockets UDP-TCP

  2. Couche application Certains transparents proviennent de: Computer Networking: A Top Down Approach, Jim Kurose, Keith RossAddison-Wesley Computer Networking: A Top Down Approach, 5th edition. Jim Kurose, Keith RossAddison-Wesley, April 2009. M2-Internet

  3. M2-Internet

  4. Présentation générale: Modèle des services de la couche transport Modèle client-serveur Modèle pair-à-pair Socket: UDP TCP Protocoles Web HTTP FTP SMTP / POP3 / IMAP DNS Pair à pair Couche Application M2-Internet

  5. e-mail web instant messaging remote login P2P file sharing multi-user network games streaming stored video clips social networks voice over IP real-time video conferencing grid computing Some network apps M2-Internet

  6. write programs that run on (different) end systems communicate over network e.g., web server software communicates with browser software No need to write software for network-core devices Network-core devices do not run user applications applications on end systems allows for rapid app development, propagation application transport network data link physical application transport network data link physical application transport network data link physical Creating a network app M2-Internet

  7. Couche application • Présentation générale: • Modèle des services de la couche transport • Modèle client-serveur • Modèle pair-à-pair • Socket: UDP TCP • Protocoles • Web HTTP • FTP • SMTP / POP3 / IMAP • DNS • Pair à pair M2-Internet

  8. Application architectures • Client-server • Including data centers / cloud computing • Peer-to-peer (P2P) • Hybrid of client-server and P2P M2-Internet

  9. client/server Client-server architecture server: • always-on host • permanent IP address • server farms for scaling clients: • communicate with server • may be intermittently connected • may have dynamic IP addresses • do not communicate directly with each other M2-Internet

  10. Google Data Centers • Estimated cost of data center: $600M • Google spent $2.4B in 2007 on new data centers • Each data center uses 50-100 megawatts of power M2-Internet

  11. peer-peer Pure P2P architecture • no always-on server • arbitrary end systems directly communicate • peers are intermittently connected and change IP addresses Highly scalable but difficult to manage M2-Internet

  12. Hybrid of client-server and P2P Skype • voice-over-IP P2P application • centralized server: finding address of remote party: • client-client connection: direct (not through server) Instant messaging • chatting between two users is P2P • centralized service: client presence detection/location • user registers its IP address with central server when it comes online • user contacts central server to find IP addresses of buddies M2-Internet

  13. Process: program running within a host. within same host, two processes communicate using inter-process communication (defined by OS). processes in different hosts communicate by exchanging messages Client process: process that initiates communication Server process: process that waits to be contacted Processes communicating • Note: applications with P2P architectures have client processes & server processes M2-Internet

  14. Sockets application application socket controlled by app developer process process transport transport controlled by OS network network link Internet link physical physical M2-Internet

  15. host or server host or server process process socket socket TCP with buffers, variables TCP with buffers, variables Sockets • process sends/receives messages to/from its socket • socket analogous to door • sending process shoves message out door • sending process relies on transport infrastructure on other side of door which brings message to socket at receiving process controlled by app developer Internet controlled by OS • API: (1) choice of transport protocol; (2) ability to fix a few parameters (lots more on this later) M2-Internet

  16. Types of messages exchanged, e.g., request, response Message syntax: what fields in messages & how fields are delineated Message semantics meaning of information in fields Rules for when and how processes send & respond to messages Public-domain protocols: defined in RFCs allows for interoperability e.g., HTTP, SMTP, BitTorrent Proprietary protocols: e.g., Skype, ppstream App-layer protocol defines M2-Internet

  17. Data integrity some apps (e.g., audio) can tolerate some loss other apps (e.g., file transfer, telnet) require 100% reliable data transfer Timing some apps (e.g., Internet telephony, interactive games) require low delay to be “effective” What transport service does an app need? • Throughput • some apps (e.g., multimedia) require minimum amount of throughput to be “effective” • other apps (“elastic apps”) make use of whatever throughput they get • Security • Encryption, data integrity, … M2-Internet

  18. Transport service requirements of common apps Time Sensitive no no no yes, 100’s msec yes, few secs yes, 100’s msec yes and no Application file transfer e-mail Web documents real-time audio/video stored audio/video interactive games instant messaging Throughput elastic elastic elastic audio: 5kbps-1Mbps video:10kbps-5Mbps same as above few kbps up elastic Data loss no loss no loss no loss loss-tolerant loss-tolerant loss-tolerant no loss M2-Internet

  19. TCP service: connection-oriented: setup required between client and server processes reliable transport between sending and receiving process flow control: sender won’t overwhelm receiver congestion control: throttle sender when network overloaded does not provide: timing, minimum throughput guarantees, security UDP service: unreliable data transfer between sending and receiving process does not provide: connection setup, reliability, flow control, congestion control, timing, throughput guarantee, or security Q: why bother? Why is there a UDP? Internet transport protocols services M2-Internet

  20. Internet apps: application, transport protocols Application layer protocol SMTP [RFC 2821] Telnet [RFC 854] HTTP [RFC 2616] FTP [RFC 959] HTTP (eg Youtube), RTP [RFC 1889] SIP, RTP, proprietary (e.g., Skype) Underlying transport protocol TCP TCP TCP TCP TCP or UDP typically UDP Application e-mail remote terminal access Web file transfer streaming multimedia Internet telephony M2-Internet

  21. Securing TCP TCP & UDP • no encryption • cleartextpasswds sent into socket traverse Internet in cleartext SSL • provides encrypted TCP connection • data integrity • end-point authentication SSL is at app layer • Apps use SSL libraries, which “talk” to TCP SSL socket API • cleartext passwds sent into socket traverse Internet encrypted Application Layer

  22. Chapitre 2: Couche application • Présentation générale: • Modèle des services de la couche transport • Modèle client-serveur • Modèle pair-à-pair • Socket: UDP TCP • Protocoles • Web HTTP • FTP • SMTP / POP3 / IMAP • DNS • Pair à pair M2-Internet

  23. Socket programming socket: door between application process and end-end-transport protocol application application socket controlled by app developer process process transport transport controlled by OS network network link Internet link physical physical Application Layer

  24. Socket programming 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: • UDP • TCP M2-Internet Java

  25. Socket programming basics • Server must be running before client can send anything to it. • Server must have a socket (door) through which it receives and sends segments • Similarly client needs a socket • Socket is locally identified with a port number • Analogous to the apt # in a building • Client needs to know server IP address and socket port number. M2-Internet Java

  26. Socket programming Two socket types for two transport services: • UDP:unreliable datagram • TCP: reliable, byte stream-oriented Application Example: • Client reads a line of characters (data) from its keyboard and sends the data to the server. • The server receives the data and converts characters to uppercase. • The server sends the modified data to the client. • The client receives the modified data and displays the line on its screen. Application Layer

  27. UDP: no “connection” between client and server no handshaking sender explicitly attaches IP address and port of destination to each segment OS attaches IP address and port of sending socket to each segment Server can extract IP address, port of sender from received segment UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server application viewpoint Socket programming with UDP M2-Internet Java

  28. Running example • Client: • User types line of text • Client program sends line to server • Server: • Server receives line of text • Capitalizes all the letters • Sends modified line to client • Client: • Receives line of text • Displays M2-Internet Java

  29. Client create socket, Create datagram with server IP and port=x; send datagram viaclientSocket clientSocket = DatagramSocket() read datagram from serverSocket write reply to serverSocket specifying client address, port number read datagram from clientSocket close clientSocket Client/server socket interaction: UDP Server (running on hostid) create socket, port= x. serverSocket = DatagramSocket() M2-Internet Java

  30. Example: Java client (UDP) Client process Input: receives packet (recall thatTCP received “byte stream”) Output: sends packet (recall that TCP sent “byte stream”) client UDP socket M2-Internet Java

  31. Example: Java client (UDP) import java.io.*; import java.net.*; class UDPClient { public static void main(String args[]) throws Exception { 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(); sendData = sentence.getBytes(); Create input stream Create client socket Translate hostname to IP address using DNS M2-Internet Java

  32. Example: Java client (UDP), cont. Create datagram with data-to-send, length, IP addr, port DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); } } Send datagram to server Read datagram from server M2-Internet Java

  33. Example: Java server (UDP) import java.io.*; import java.net.*; class UDPServer { public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(9876); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; while(true) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); Create datagram socket at port 9876 Create space for received datagram Receive datagram M2-Internet Java

  34. Example: Java server (UDP), cont String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase(); sendData = capitalizedSentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); } } } Get IP addr port #, of sender Create datagram to send to client Write out datagram to socket End of while loop, loop back and wait for another datagram M2-Internet Java

  35. UDP observations & questions • Both client server use DatagramSocket • Dest IP and port are explicitly attached to segment. • What would happen if change both clientSocket and serverSocket to “mySocket”? • Can the client send a segment to server without knowing the server’s IP address and/or port number? • Can multiple clients use the server? M2-Internet Java

  36. process process TCP with buffers, variables TCP with buffers, variables socket socket Socket-programming using TCP TCP service: reliable transfer of bytesfrom one process to another controlled by application developer controlled by application developer controlled by operating system controlled by operating system internet host or server host or server M2-Internet Java

  37. 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 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 source port numbers used to distinguish clients (more in Chap 3) TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server application viewpoint Socket programming with TCP M2-Internet Java

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

  39. A stream is a sequence of characters that flow into or out of a process. An input stream is attached to some input source for the process, e.g., keyboard or socket. An output stream is attached to an output source, e.g., monitor or socket. Stream jargon Client process client TCP socket M2-Internet Java

  40. 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) Socket programming with TCP M2-Internet Java

  41. 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)); Socket clientSocket = new Socket("hostname", 6789); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); Create input stream Create client socket, connect to server Create output stream attached to socket M2-Internet Java

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

  43. 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 Wait, on welcoming socket for contact by client Create input stream, attached to socket M2-Internet Java

  44. Example: Java server (TCP), cont DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + '\n'; outToClient.writeBytes(capitalizedSentence); } } } Create output stream, attached to socket Read in line from socket Write out line to socket End of while loop, loop back and wait for another client connection M2-Internet Java

  45. TCP observations & questions • Server has two types of sockets: • ServerSocket and Socket • When client knocks on serverSocket’s “door,” server creates connectionSocket and completes TCP conx. • Dest IP and port are not explicitly attached to segment. • Can multiple clients use the server? M2-Internet Java

  46. Sockets et java • Rappels Java • Rappels généraux: streams, threads • Sockets java M2-Internet

  47. Entrées-sorties java • Streams • Output streams • Input streams • Filter streams • Readers et writer • (non blocking I/O) M2-Internet Java

  48. OuputStream • public abstract class OutputStream • public abstract void write(int b) throws IOException • public void write(byte[] data) throws IOException • Public void write(byte[] data, int offset, int length) throws IOException • public void flush( ) throws IOException • public void close( ) throws IOException M2-Internet Java

  49. InputStream • public abstract class InputStream • public abstract int read( ) throws IOException • public int read(byte[] input) throws IOException • public int read(byte[] input, int offset, int length) throws IOException • public long skip(long n) throws IOException • public int available( ) throws IOException • public void close( ) throws IOException • public void mark(int readAheadLimit) • public void reset( ) throws IOException • public boolean markSupported( ) M2-Internet Java

  50. Lecture: int bytesRead=0; int bytesToRead=1024; byte[] input = new byte[bytesToRead]; while (bytesRead < bytesToRead) { int result = in.read(input, bytesRead, bytesToRead - bytesRead); if (result == -1) break; bytesRead += result; } M2-Internet Java