95-702 Distributed Systems Information System Management 1 Organizational Communications and Distributed Object Technologies Lecture 8 Chapter 4: Inter-process Communications95-702 Distributed Systems Information System Management 2 Middleware layers Applications, servicesMiddlewarelayersrequest-reply protocolmarshalling and external data representationUDP and TCPThischapterRMI and RPC95-702 Distributed Systems Information System Management 3 Socket and Port Abstractions message!agreed port!any port!socket!socket!Internet address = 138.37.88.249!Internet address = 138.37.94.248!other ports!client!server!95-702 Distributed Systems Information System Management 4 import java.net.*;!import java.io.*;!public class UDPClient{ ! public static void main(String args[]){ !!// args give message contents and server hostname!!DatagramSocket aSocket = null;!! try {!!!aSocket = new DatagramSocket(); !!!byte [] m = args[0].getBytes();!!!InetAddress aHost = InetAddress.getByName(args[1]);!!!int serverPort = 6789; ! ! !!!DatagramPacket request = new DatagramPacket(m, args[0].length(), aHost, serverPort);!!!aSocket.send(request); ! !! !!!byte[] buffer = new byte[1000];!!!DatagramPacket reply = new DatagramPacket(buffer, buffer.length);!!!aSocket.receive(reply);!!!System.out.println("Reply: " + new String(reply.getData()));!! }! catch (Exception e) ! {! System.out.println("Problem: " + e.toString());! }!! finally {! if(aSocket != null) aSocket.close();! }! } !}!A UDP Client95-702 Distributed Systems Information System Management 5 A UDP Server import java.net.*;!import java.io.*;!public class UDPServer {! public static void main(String args[]){ !!DatagramSocket aSocket = null;!!try{!! aSocket = new DatagramSocket(6789);!! byte[] buffer = new byte[1000];! ! while(true){! DatagramPacket request = new DatagramPacket(buffer, buffer.length);! aSocket.receive(request); ! System.out.println("Got request"); ! DatagramPacket reply = new DatagramPacket(request.getData(), request.getLength(), ! request.getAddress(), request.getPort());! aSocket.send(reply);!! }!! }! catch (Exception e){! System.out.println("Problem: " + e.getMessage());! }!! finally {! if(aSocket != null) aSocket.close();! }! }!}!Two Demonstrations 95-702 Distributed Systems Information System Management 6 1) Run the UDP client and server on the same machine. /Users/mm6/mm6/mm6/www/95-702/UDPNetworking java UDPServer java UDPClient hello localhost 2) Run a UDP client on an Android device. The server will run on a laptop. In this case, the UDP server will accept arithmetic expressions. Netbeans 6.8: Homework3Part1UDPProject/UDPServer.java Eclipse: AndroidUDPCalculatorProject Quiz: What if the client sends a packet and that packet is lost? Does this server handle concurrent visitors? Is the packet safe from eavesdropping? Could we visit this server with a .Net client?95-702 Distributed Systems Information System Management 7 TCP Client import java.net.*; import java.io.*; public class TCPClient { public static void main (String args[]) { // arguments supply message and hostname of destination Socket s = null; try{ int serverPort = 7896; s = new Socket(args[1], serverPort); DataInputStream in = new DataInputStream( s.getInputStream()); DataOutputStream out = new DataOutputStream( s.getOutputStream()); out.writeUTF(args[0]); // UTF is a string encoding see Sn 4.3 String data = in.readUTF(); System.out.println("Received: "+ data) ; } catch (Exception e) { System.out.println("Trouble: " + e.getMessage()); } finally { if(s!=null) try {s.close();} catch (IOException e) { System.out.println("close:"+e.getMessage()); } } } }95-702 Distributed Systems Information System Management 8 TCP Server(1) import java.net.*;!import java.io.*;! public class TCPServer {! public static void main (String args[]) {!!try {!! int serverPort = 7896; !! ServerSocket listenSocket = new ServerSocket(serverPort);!! while(true) {!!!Socket clientSocket = listenSocket.accept();! System.out.println("Got connection");!!!Connection c = new Connection(clientSocket);!! }!!} ! catch(IOException e) {! System.out.println("Listen :"+e.getMessage());! }! }!}!95-702 Distributed Systems Information System Management 9 TCP Server(2) class Connection extends Thread { DataInputStream in; DataOutputStream out; Socket clientSocket; public Connection (Socket aClientSocket) { try { clientSocket = aClientSocket; in = new DataInputStream( clientSocket.getInputStream()); out =new DataOutputStream( clientSocket.getOutputStream()); this.start(); } catch(IOException e) {System.out.println("Connection:"+e.getMessage());} } public void run() { try { String data = in.readUTF(); out.writeUTF("From server: " + data); } catch(Exception e) { System.out.println("EOF:"+e.getMessage()); } finally{ try {clientSocket.close();}catch (IOException e){/*close failed*/}} } }Demonstration 95-702 Distributed Systems Information System Management 10 /Users/mm6/mm6/mm6/www/95-702/TCPNetworking java TCPServer java TCPClient hello localhost95-702 Distributed Systems Information System Management 11 Quiz What if the client sends a packet and that packet is lost? Does this server handle concurrent visitors? Is the packet safe from eavesdropping? Could we visit
View Full Document