1
play

1 Example: Java client (TCP) Example: Java client (TCP), cont. - PDF document

Socket-programming using TCP Socket: a door between application process and end- end-transport protocol (UCP or TCP) TCP service: reliable transfer of bytes from one Socket Programming process to another controlled by controlled by process


  1. Socket-programming using TCP Socket: a door between application process and end- end-transport protocol (UCP or TCP) TCP service: reliable transfer of bytes from one Socket Programming process to another controlled by controlled by process application process application developer socket developer socket TCP with controlled by TCP with controlled by buffers, operating operating buffers, internet system variables system variables host or host or server server 2: Application Layer 2: Application Layer 1 2 Socket programming with TCP Stream jargon Client must contact server When contacted by client, keyboard monitor ❒ ❒ A stream is a sequence server process must first be server TCP creates new socket ❒ of characters that flow for server process to running inFromUser communicate with client input server must have created into or out of a process. ❒ stream socket (door) that welcomes ❍ allows server to talk with Client Process multiple clients ❒ An input stream is client’s contact process ❍ source port numbers used attached to some input Client contacts server by: to distinguish clients (more creating client-local TCP source for the process, ❒ in Chap 3) socket inFromServer eg, keyboard or socket. outToServer specifying IP address, port output input ❒ application viewpoint stream stream number of server process ❒ An output stream is TCP provides reliable, in-order When client creates socket: ❒ attached to an output client TCP client TCP establishes transfer of bytes (“pipe”) clientSocket socket TCP source, eg, monitor or connection to server TCP between client and server socket socket. to network from network 2: Application Layer 2: Application Layer 3 4 Socket programming with TCP Client/server socket interaction: TCP Server (running on hostid ) Client keyboard monitor Example client-server app: create socket, 1) client reads line from inFromUser port= x , for input standard input ( inFromUser incoming request: stream welcomeSocket = Client stream) , sends to server via ServerSocket() Process TCP socket ( outToServer process create socket, wait for incoming connection setup stream) connect to hostid , port= x connection request clientSocket = connectionSocket = Socket() 2) server reads line from socket welcomeSocket.accept() inFromServer outToServer 3) server converts line to send request using output input read request from clientSocket uppercase, sends back to stream stream connectionSocket client client TCP write reply to clientSocket connectionSocket 4) client reads, prints modified socket read reply from TCP clientSocket socket line from socket close to network from network close connectionSocket ( inFromServer stream) clientSocket 2: Application Layer 5 2: Application Layer 6 1

  2. Example: Java client (TCP) Example: Java client (TCP), cont. import java.io.*; import java.net.*; Create BufferedReader inFromServer = class TCPClient { input stream new BufferedReader(new attached to socket InputStreamReader(clientSocket.getInputStream())); public static void main(String argv[]) throws Exception { sentence = inFromUser.readLine(); String sentence; Send line String modifiedSentence; outToServer.writeBytes(sentence + '\n'); to server Create BufferedReader inFromUser = Read line input stream modifiedSentence = inFromServer.readLine(); new BufferedReader(new InputStreamReader(System.in)); from server Create System.out.println ("FROM SERVER: " + modifiedSentence ); client socket, Socket clientSocket = new Socket("hostname", 6789); connect to server clientSocket.close(); Create DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); output stream } attached to socket } 2: Application Layer 2: Application Layer 7 8 Example: Java server (TCP) Example: Java server (TCP), cont import java.io.*; import java.net.*; Create output class TCPServer { stream, attached DataOutputStream outToClient = to socket new DataOutputStream (connectionSocket.getOutputStream()); public static void main(String argv[]) throws Exception { Read in line clientSentence = inFromClient.readLine(); String clientSentence; from socket Create String capitalizedSentence; welcoming socket capitalizedSentence = clientSentence.toUpperCase() + '\n'; ServerSocket welcomeSocket = new ServerSocket(6789); at port 6789 Write out line outToClient.writeBytes(capitalizedSentence); Wait, on welcoming while(true) { to socket } socket for contact } Socket connectionSocket = welcomeSocket.accept(); by client End of while loop, } loop back and wait for BufferedReader inFromClient = Create input new BufferedReader(new another client connection stream, attached InputStreamReader(connectionSocket.getInputStream())); to socket 2: Application Layer 2: Application Layer 9 10 Socket programming with UDP Client/server socket interaction: UDP Server (running on hostid ) Client UDP: no “connection” between client and server create socket, create socket, port= x , for ❒ no handshaking clientSocket = incoming request: DatagramSocket() application viewpoint serverSocket = ❒ sender explicitly attaches DatagramSocket() UDP provides unreliable transfer IP address and port of Create, address ( hostid, port=x, of groups of bytes (“datagrams”) send datagram request destination to each packet using clientSocket read request from between client and server ❒ server must extract IP serverSocket address, port of sender write reply to from received packet serverSocket read reply from specifying client clientSocket host address, UDP: transmitted data may port number close be received out of order, clientSocket or lost 2: Application Layer 11 2: Application Layer 12 2

  3. Example: Java client (UDP) Example: Java client (UDP) keyboard monitor import java.io.*; inFromUser import java.net.*; input stream class UDPClient { Client public static void main(String args[]) throws Exception Process Input: receives { Create process packet (TCP input stream BufferedReader inFromUser = Output: sends received “byte Create new BufferedReader(new InputStreamReader(System.in)); stream”) packet (TCP sent sendPacket receivePacket client socket “byte stream”) DatagramSocket clientSocket = new DatagramSocket(); UDP UDP packet packet Translate InetAddress IPAddress = InetAddress.getByName("hostname"); hostname to IP client UDP clientSocket address using DNS socket byte[] sendData = new byte[1024]; UDP socket byte[] receiveData = new byte[1024]; to network from network String sentence = inFromUser.readLine(); sendData = sentence.getBytes(); 2: Application Layer 2: Application Layer 13 14 Example: Java client (UDP), cont. Example: Java server (UDP) Create datagram import java.io.*; with data-to-send, DatagramPacket sendPacket = import java.net.*; length, IP addr, port new DatagramPacket(sendData, sendData.length, IPAddress, 9876); class UDPServer { Send datagram clientSocket.send(sendPacket); public static void main(String args[]) throws Exception to server Create { DatagramPacket receivePacket = datagram socket new DatagramPacket(receiveData, receiveData.length); DatagramSocket serverSocket = new DatagramSocket(9876); at port 9876 Read datagram clientSocket.receive(receivePacket); byte[] receiveData = new byte[1024]; from server byte[] sendData = new byte[1024]; String modifiedSentence = new String(receivePacket.getData()); while(true) { System.out.println("FROM SERVER:" + modifiedSentence); Create space for clientSocket.close(); DatagramPacket receivePacket = received datagram } new DatagramPacket(receiveData, receiveData.length); } Receive serverSocket.receive(receivePacket); datagram 2: Application Layer 2: Application Layer 15 16 Building a simple Web server Example: Java server (UDP), cont String sentence = new String(receivePacket.getData()); ❒ handles one HTTP ❒ after creating server, Get IP addr InetAddress IPAddress = receivePacket.getAddress(); port #, of you can request file request sender int port = receivePacket.getPort(); using a browser (eg IE ❒ accepts the request explorer) String capitalizedSentence = sentence.toUpperCase(); ❒ parses header sendData = capitalizedSentence.getBytes(); ❒ see text for details ❒ obtains requested file Create datagram DatagramPacket sendPacket = to send to client from server’s file new DatagramPacket(sendData, sendData.length, IPAddress, port); system Write out datagram serverSocket.send(sendPacket); ❒ creates HTTP response to socket } } message: } End of while loop, ❍ header lines + file loop back and wait for another datagram ❒ sends response to client 2: Application Layer 17 2: Application Layer 18 3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend