Distributed Computing

Slides:



Advertisements
Similar presentations
CS Network Programming CS 3331 Fall CS 3331 Outline Socket programming Remote method invocation (RMI)
Advertisements

Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project.
Referring to Java API Specifications
Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server.
School of Information Technologies Multithreading NETS3303/3603 Week 3.
9. Exceptions An exception occurs when our code asks the JVM to perform the technically impossible and unanticipated (by the compiler)! Under these circumstances,
Lecture 10: Networking using Socket Programming. Client-Server Model b The term server applies to any program that offers a service that can be reached.
COMP201 Java Programming Part III: Advanced Features Topic 14: Networking Volume II,Chapter 3.
Java Threads A tool for concurrency. OS schedules processes Ready Running 200 Blocked A process loses the CPU and another.
Programming Applets How do Applets work ? This is an HTML page This is the applet’s code It has a link to an applet.
Java Networking -- Socket Server socket class: ServerSocket wait for requests from clients. after a request is received, a client socket is generated.
System Programming Practical session 10 Java sockets.
Client/Server example. Server import java.io.*; import java.net.*; import java.util.*; public class PrimeServer { private ServerSocket sSoc; public static.
CIS – Spring Instructors: Geoffrey Fox, Bryan Carpenter Computational Science and.
Java sockets. From waiting.
Software Engineering Recitation 2 Suhit Gupta. Today we will be covering… XML II Sockets, Server – Client relationships, Servers capable of handling multiple.
System Programming Practical session 11 Multiple clients server Non-Blocking I/O.
1 Fall 2005 Socket Programming Qutaibah Malluhi Computer Science and Engineering Qatar University.
1 First app – simplechat1 Reminder: what’s the point?  To build on ocsf framework.  Simple requirements: echo all messages to all clients.  Use only.
CSE 341, S. Tanimoto Java networking- 1 Java Networking Motivation Network Layers Using Sockets A Tiny Server Applets URLs Downloading Images, MediaTracker.
EE2E1. JAVA Programming Lecture 9 Network Programming.
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
Import java.net.*; import java.io.*; public class LowPortScanner { public static void main(String[] args) { String host = "localhost"; if (args.length.
Cli/Serv.: Chat/121 Client/Server Distributed Systems v Objectives –discuss a client/server based chat system –mention two other ways of chatting.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 RMI.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
Lecture 9 Network programming. Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.
MSc Course in Advanced Distributed Systems Session 2.2: Practical CORBA Programming
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 22.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/11) Java Sockets and Simple Networking Joel Adams and Jeremy Frens.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Networking and Concurrency COMP.
1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Computer Networks & PROTOCOLS (CSI 4118) FALL 2005 Professor Robert L. Probert.
Java Server Programming Web Interface for Java Programs.
Part 4: Network Applications Client-server interaction, example applications.
By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress.
1 Lecture 9: Network programming. 2 Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on.
Java Server Sockets ServerSocket : Object to listen for client connection requests Throws IOException accept() method to take the client connection. Returns.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Topics to be covered: Java Packages Thread Example Java Socket Programming.
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
Networking Code CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
第十一讲 分布式编程 Socket and RMI 李庆旭. 2 本章内容提要  Socket TCP /IP protocol suit Socket A TCP Server and a TCP Client Supporting Multiple Concurrent Clients A UDP.
Java Networking I IS Outline  Quiz #3  Network architecture  Protocols  Sockets  Server Sockets  Multi-threaded Servers.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
CS 5204 Fall 00 1 Distributed Programming low level: sending data among distributed computations higher level: supporting invocations among distributed.
Stock Market Quotes – Socket Version
Echo Networking COMP
Threads in Java Two ways to start a thread
Network Programming in Java CS 1111 Ryan Layer May 3, 2010
Lecture 21 Sockets 1 (Not in D&D) Date.
CSE 341, S. Tanimoto Java networking-
Block 15 Developing the Calculator application
Block 14 Group Communication (Multicast)
An Introduction to TCP/IP and Sockets
Lecture 9 Network Programming
„Networking”.
EE 422C Socket Programming
CS 390 Unix Programming Environment
CSI 4118 – UNIVERSITY OF OTTAWA
Clients and Servers 19-Jul-19.
CS18000: Problem Solving and Object-Oriented Programming
Clients and Servers 13-Sep-19.
Presentation transcript:

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

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” 127.0.0.1, but they will work to any IP address

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

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

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

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

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

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?

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

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

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