Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 COMP 431 Internet Services & Protocols Client/Server Computing & Socket Programming Jasleen Kaur February 2, 2016.

Similar presentations


Presentation on theme: "1 COMP 431 Internet Services & Protocols Client/Server Computing & Socket Programming Jasleen Kaur February 2, 2016."— Presentation transcript:

1 1 COMP 431 Internet Services & Protocols Client/Server Computing & Socket Programming Jasleen Kaur February 2, 2016

2 2 Application-Layer Protocols Overview u Application-layer protocols define: »The types of messages exchanged »The syntax and semantics of messages »The rules for when and how messages are sent u Public protocols (defined in RFCs) »HTTP, FTP, SMTP, POP, IMAP, DNS u Proprietary protocols »RealAudio, RealVideo »IP telephony »… local ISP company network regional ISP application transport network link physical application

3 3 Application-Layer Protocols Outline u The architecture of distributed systems »Client/Server computing u The programming model used in constructing distributed systems »Socket programming u Example client/server systems and their application-level protocols »The World-Wide Web (HTTP) »Reliable file transfer (FTP) »E-mail (SMTP & POP) »Internet Domain Name System (DNS) local ISP company network regional ISP application transport network link physical application

4 4 Application-Layer Protocols Outline local ISP company network regional ISP u Protocol design issues: »In-band vs. out-of-band control signaling »Push vs. pull protocols »Persistent vs. non-persistent connections u Client/server service architectures »Contacted server responds vs. forwards request u Example client/server systems and their application-level protocols »The World-Wide Web (HTTP) »Reliable file transfer (FTP) »E-mail (SMTP & POP) »Internet Domain Name System (DNS) application transport network link physical application

5 5 local ISP company network regional ISP The Application Layer The client-server paradigm u Typical network application has two pieces: client and server u Client: »Initiates contact with server (“speaks first”) »Requests service from server »For Web, client is implemented in browser; for e-mail, in mail reader u Server: »Provides requested service to client »“Always” running »May also include a “client interface” reply request Client Server application transport network link physical application transport network link physical application

6 6 Client/Server Paradigm Socket programming u Sockets are the fundamental building block for client/server systems u Sockets are created and managed by applications »Strong analogies with files a host-local, application created/released, OS-controlled interface into which an application process can both send and receive messages to/from another (remote or local) application process socket u Two types of transport services are available via the socket API: »UDP sockets: unreliable, datagram-oriented communications »TCP sockets: reliable, stream-oriented communications

7 7 Client/Server Paradigm Socket-programming using TCP u A socket is an application created, OS-controlled interface into which an application can both send and receive messages to and from another application »A “door” between application processes and end-to-end transport protocols process TCP with buffers, variables socket controlled by application developer controlled by operating system Host (end system) process TCP with buffers, variables socket controlled by application developer controlled by operating system Host (end system) Internet

8 8 Socket-programming using TCP TCP socket programming model u A TCP socket provides a reliable, bi-directional, byte-stream communications channel from one process to another »A “pair of pipes” abstraction Process socket Host (end system) Host (end system) Internet Process socket bytes write read writeread

9 9 TCP with buffers, variables TCP with buffers, variables Socket-programming using TCP Network addressing for sockets process socket End System process socket Local port numbers (e.g., 6500) Internet domain name of host e.g., classroom.cs.unc.edu DNS u Sockets are addressed using an IP address and port number Internet addresses of hosts (e.g., 152.2.131.245)

10 10 Socket-programming using TCP Socket programming in Java u When the client creates a socket, the client’s TCP establishes connection to server’s TCP u When contacted by a client, server creates a new socket for server process to communicate with client »This allows the server to talk with multiple clients u Client creates a local TCP socket specifying the host and port number of server process »Java resolves host names to IP addresses using DNS u Client contacts server »Server process must be running »Server must have created socket that “welcomes” client’s contact Client socket Internet Server socket bytes write read writeread

11 11 Socket-programming using TCP Socket creation in the client-server model process host or server Internet process bytes TCP 3-way handshake Client Server 3-way handshake Client client socket “welcoming” socket connection socket bytes socket

12 12 Socket-programming using TCP Simple client-server example u The client reads a line of text from standard input and sends the text to the server via a socket u The server receives the line of text from the client and converts the line of characters to all uppercase u The server sends the converted line back to the client u The client receives the converted text and writes it to standard output Server welcoming socket connection socket Client client socket stdin stdout

13 13 Socket programming with TCP Example Client structure  Client reads from standard input ( inFromUser stream), writes to server via a socket ( outToServer stream) u Server reads line from a socket u Server converts line to uppercase and writes back to client  Client reads from socket, ( inFromServer stream) prints modified line to standard output client socket inFromServer outToServer inFromUser Client Process (1) (2) (3) Standard output (4) Standard input

14 14 Socket programming with TCP Example Client/server TCP socket interaction in Java create socket for incoming request (port=6789) welcomeSocket = new ServerSocket(...) wait for incoming connection request connectionSocket = welcomeSocket.accept() create socket, connect to swan.cs.unc.edu, port=6789 clientSocket = new Socket(...) close connectionSocket close clientSocket Server (running on swan.cs.unc.edu) Client (running on classroom.cs...) read reply from clientSocket write request using clientSocket read request from connectionSocket write reply to connectionSocket TCP connection setup program flow data flow …

15 15 Socket programming with TCP Example Java client 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)); System.out.println("Client ready for input"); while ((sentence = inFromUser.readLine()) != null) { Socket clientSocket = new Socket("swan.cs.unc.edu", 6789); // Create (buffered) input stream using standard input // While loop to read and handle multiple input lines // Create client socket with connection to server at port 6789

16 16 Socket programming with TCP Example Java client II DataOutputStream outToServer = new DataOutputStream( clientSocket.getOutputStream()); BufferedReader inFromServer = new BufferedReader( new InputStreamReader( clientSocket.getInputStream())); outToServer.writeBytes(sentence + '\n'); modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER: " + modifiedSentence); clientSocket.close(); } } // end main } // end class // Create output stream attached to socket // Create (buffered) input stream attached to socket // Write line to server // Read line from server // end while, loop to accept more lines from user

17 17 Socket programming with TCP Example Java server 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); System.out.println("Server Ready for Connection"); while(true) { Socket connectionSocket = welcomeSocket.accept(); System.out.println("Client Made Connection"); // Create “welcoming” socket using port 6789 // While loop to handle arbitrary sequence of clients making requests // Waits for a client to connect and creates new socket for connection

18 18 Socket programming with TCP Example Java server II BufferedReader inFromClient = new BufferedReader( new InputStreamReader( connectionSocket.getInputStream())); DataOutputStream outToClient = new DataOutputStream( connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); System.out.println("Client sent: " + clientSentence); capitalizedSentence = clientSentence.toUpperCase() + '\n'; outToClient.writeBytes(capitalizedSentence); connectionSocket.close(); } } // end main } // end class // Create (buffered) input stream attached to connection socket // Create output stream attached to connection socket // Read input line from socket // Write output line to socket // end while; loop back to accept a new client connection

19 19 Socket-programming using UDP UDP socket programming model u A UDP socket provides an unreliable bi-directional communication channel from one process to another »A “datagram” abstraction Host (end system) Host (end system) Process socket Internet Process socket bytes write read writeread

20 20 Socket programming with UDP Example Client/server UDP socket interaction in Java create socket for incoming request (port=9876) serverSocket = new DatagramSocket() create socket, clientSocket = new DatagramSocket() read reply from clientSocket create address (152.2.131.245, port = 9876) and send datagram using clientSocket read request from serverSocket write reply to serverSocket specifying client IP address and port number program flow data flow Server (running on 152.2.131.245) Client close clientSocket

21 21 Socket Programming Services provided by Internet transport protocols u TCP service: »connection-oriented: setup required between client, server »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 bandwidth guarantees u UDP service: »unreliable data transfer between sending and receiving process »does not provide: connection setup, reliability, flow control, congestion control, timing, or minimum bandwidth guarantees Why bother? Why is there a UDP?


Download ppt "1 COMP 431 Internet Services & Protocols Client/Server Computing & Socket Programming Jasleen Kaur February 2, 2016."

Similar presentations


Ads by Google