Presentation is loading. Please wait.

Presentation is loading. Please wait.

Distributed Computing

Similar presentations


Presentation on theme: "Distributed Computing"— Presentation transcript:

1 Distributed Computing
It is becoming increasingly rare to run programs that don’t involve distributed computing in some way We will study socket communications under the explicit control of the programmer Other techniques are available, such as Remote Method Invocation (RMI), but they are beyond the scope of this course

2 Socket Communications
Characteristics The most widely available technique, you can communicate with non-Java applications via sockets It is also a primitive technique where you deal with the network directly You will need to import java.net.* We will develop a sequence of examples Each simple example will expand functionality These will be demonstrated using “localhost” , but they will work to any IP address

3 A First Example - 1 class EchoServer {
public static void main(String[] args) { System.out.println("EchoServer started."); try { ServerSocket s = new ServerSocket(8008); Socket incoming = s.accept(); System.out.println("Connected to: " + incoming.getInetAddress() + " at port: " + incoming.getLocalPort()); BufferedReader in = new BufferedReader(new InputStreamReader (incoming.getInputStream())); PrintWriter out = new PrintWriter(new OutputStreamWriter (incoming.getOutputStream())); out.println("Hello! This is Java EchoServer. Enter BYE to exit."); out.flush(); for (;;) { String str = in.readLine(); if (str == null) { break; } else { out.println("Echo: " + str); System.out.println("Received: " + str); if (str.trim().equals("BYE")) break; } incoming.close(); } catch (Exception e) { System.out.println("Error: " + e); } System.out.println("EchoServer stopped."); A First Example - 1

4 Running This Example We will use telnet to run this program
Here is a sample session for the client telnet localhost 8008 Hello! This is the Java EchoServer. Enter BYE to exit. Hi, who are you? Echo: Hi, who are you? You have to answer first. Echo: You have to answer first. No, YOU GO FIRST. Echo: No, YOU GO FIRST. Oh, I can't stand you. Echo: Oh, I can't stand you. BYE Echo: BYE

5 A Second Example class EchoClient {
public static void main(String[] args) { try { String host; if (args.length > 0 && args[0] != null) { host = args[0]; } else { host = "localhost"; } Socket t = new Socket(host, 8008); BufferedReader in = new BufferedReader (new InputStreamReader(t.getInputStream())); PrintWriter out = new PrintWriter (new OutputStreamWriter(t.getOutputStream())); for (int i = 1; i <= 10; i++) { System.out.println("Sending: line " + i); out.println("line " + i); out.flush(); } out.println("BYE"); out.flush(); for (;;) { String str = in.readLine(); if (str == null) { break; } else { System.out.println(str); } } } catch (Exception e) { System.out.println("Error: " + e); A Second Example

6 What Happens When This Example Runs?
How do you run the program? What will happen when it runs?

7 Handling Multiple Clients - 1
class ClientHandler extends Thread { protected Socket incoming; public ClientHandler(Socket incoming) { this.incoming = incoming; } public void run() { try { BufferedReader in = new BufferedReader (new InputStreamReader(incoming.getInputStream())); PrintWriter out = new PrintWriter (new OutputStreamWriter(incoming.getOutputStream())); out.println("Hello! This is Java MultiEchoServer."); out.println("Enter BYE to exit."); out.flush(); for (;;) { String str = in.readLine(); if (str == null) { break; } else { out.println("Echo: " + str); out.flush(); System.out.println("Received: " + str); if (str.trim().equals("BYE")) break; } incoming.close(); } catch (Exception e) { System.out.println("Error: " + e); Handling Multiple Clients - 1

8 Handling Multiple Clients - 2
public class MultiEchoServer { public static void main(String[] args) { System.out.println("MultiEchoServer started."); try { ServerSocket s = new ServerSocket(8009); for (;;) { Socket incoming = s.accept(); new ClientHandler(incoming).start(); } } catch (Exception e) { System.out.println("Error: " + e); System.out.println("MultiEchoServer stopped."); How do you start this program running? What happens after it starts running?

9 EchoServer with Broadcast - 1
class BroadcastClientHandler extends Thread { protected Socket incoming; protected int id; protected BufferedReader in; protected PrintWriter out; public BroadcastClientHandler(Socket incoming, int id) { this.incoming = incoming; this.id = id; try { if (incoming != null) { in = new BufferedReader(new InputStreamReader(incoming.getInputStream())); out = new PrintWriter(new OutputStreamWriter(incoming.getOutputStream())); } } catch (Exception e) { System.out.println("Error: " + e); public synchronized void putMessage(String msg) { if (out != null) { out.println(msg); out.flush(); EchoServer with Broadcast - 1

10 EchoServer with Broadcast - 2
public void run() { System.out.println("Client handler " + id + " started."); if (in != null && out != null) { putMessage("Hello! This is Java BroadcastEchoServer. Enter BYE to exit."); try { for (;;) { String str = in.readLine(); if (str == null) { break; } else { putMessage("Echo: " + str); System.out.println("Received (" + id + "): " + str); if (str.trim().equals("BYE")) { break; } \ else { Enumeration enum = BroadcastEchoServer.activeThreads.elements(); while (enum.hasMoreElements()) { BroadcastClientHandler t = (BroadcastClientHandler)enum.nextElement(); if (t != this) { t.putMessage("Broadcast(" + id + "): " + str); } } } } } incoming.close(); BroadcastEchoServer.activeThreads.removeElement(this); } catch (IOException e) {} System.out.println("Client thread " + id + " stopped."); EchoServer with Broadcast - 2

11 EchoServer with Broadcast - 3
public class BroadcastEchoServer { static protected Vector activeThreads; public static void main(String[] args) { System.out.println("BroadcastEchoServer started."); activeThreads = new Vector(); int i = 1; try { ServerSocket s = new ServerSocket(8010); for (;;) { Socket incoming = s.accept(); System.out.println("Spawning client thread " + i); BroadcastClientHandler newThread = new BroadcastClientHandler(incoming, i); activeThreads.addElement(newThread); newThread.start(); i++; } } catch (Exception e) { System.out.println("Error: " + e); System.out.println("BroadcastEchoServer stopped."); EchoServer with Broadcast - 3


Download ppt "Distributed Computing"

Similar presentations


Ads by Google