第十一讲 分布式编程 Socket and RMI 李庆旭. 2 本章内容提要  Socket TCP /IP protocol suit Socket A TCP Server and a TCP Client Supporting Multiple Concurrent Clients A UDP.

Slides:



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

1 Chapter 9 Network Programming. 2 Overview Java has rich class libraries to support network programming at various levels. There are standard classes.
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.
15-May-15 RMI Remote Method Invocation. 2 “The network is the computer” Consider the following program organization: If the network is the computer, we.
Jan Java Networking UDP Yangjun Chen Dept. Business Computing University of Winnipeg.
Java Remote Method Invocation (RMI) In Java we implement object systems: O1O2 O3 thread 1thread 2 execution scheme JVM 1JVM 2 distribution scheme.
Network Programming Chapter 11 Lecture 6. Networks.
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.
Java Sockets Source:
Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.
Sockets  Defined as an “endpoint for communication.”  Concatenation of IP address + port.  Used for server-client communication.  Server waits for.
Introduction to Remote Method Invocation (RMI)
Java sockets. From waiting.
Java Remote Object Invocation (RMI) Overview of RMI Java RMI allowed programmer to execute remote function class using the same semantics as local functions.
Projekt współfinansowany przez Unię Europejską w ramach Europejskiego Funduszu Społecznego „Networking”
1 Fall 2005 Socket Programming Qutaibah Malluhi Computer Science and Engineering Qatar University.
Building an ftp client and server using sockets we now know enough to build a sophisticated client/server application!  ftp  telnet  smtp  http.
Babak Esfandiari (based on slides by Qusay Mahmoud)
CS 352-Socket Programming & Threads Dept. of Computer Science Rutgers University (Thanks,this slides taken from er06/
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
1 Java Programming II Java Network II (Distributed Objects in Java)
UDP vs TCP UDP Low-level, connectionless No reliability guarantee TCP Connection-oriented Not as efficient as UDP.
 TCP (Transport Control Protocol) is a connection-oriented protocol that provides a reliable flow of data between two computers.  TCP/IP Stack Application.
1 Java RMI G53ACC Chris Greenhalgh. 2 Contents l Java RMI overview l A Java RMI example –Overview –Walk-through l Implementation notes –Argument passing.
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.
RMI remote method invocation. Traditional network programming The client program sends data to the server in some intermediary format and the server has.
RMI Remote Method Invocation Distributed Object-based System and RPC Together 2-Jun-16.
1 Network Programming and Java Sockets. 2 Network Request Result a client, a server, and network Client Server Client machine Server machine Elements.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
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.
Java Remote Object Invocation (RMI) Overview of RMI Java RMI allowed programmer to execute remote function class using the same semantics as local functions.
 Remote Method Invocation  A true distributed computing application interface for Java, written to provide easy access to objects existing on remote.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
Java Programming: Advanced Topics 1 Networking Programming Chapter 11.
VII. Sockets. 1. What is a Socket? A socket is one end-point of a two-way communication link between two programs running on the network. Socket classes.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Computer Networks & PROTOCOLS (CSI 4118) FALL 2005 Professor Robert L. Probert.
Java Remote Method Invocation (RMI) Overview of RMI Java RMI allowed programmer to execute remote function class using the same semantics as local functions.
1 Java Remote Method Invocation java.rmi.* java.rmi.registry.* java.rmi.server.*
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.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
 Java RMI Distributed Systems IT332. Outline  Introduction to RMI  RMI Architecture  RMI Programming and a Sample Example:  Server-Side RMI programming.
Java Programming II Java Network (I) Java Programming II.
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.
Remote Method Invocation A Client Server Approach.
Java Server Sockets ServerSocket : Object to listen for client connection requests Throws IOException accept() method to take the client connection. Returns.
1 CSCD 330 Network Programming Fall 2013 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 8a Application.
UMBC Distributed Computing with Objects RMI/Corba CMSC 432 Shon Vick.
Remote Method Invocation RMI architecture stubs and skeletons for remote services RMI server and client in Java Creating an RMI Application step-by- step.
Khoa CNTT 1/37 PHẠM VĂN TÍNH   Java RMI (Remote Method Invocation)
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
CSC 480 Software Engineering Lab 6 – RMI Nov 8, 2002.
Netprog Java RMI1 Remote Method Invocation.
RMI1 Remote Method Invocation Adapted from “Core Java 2” by Cay Horstmann.
Network Programming Communication between processes Many approaches:
Socket Programming Ameera Almasoud
Source: Java Sockets Source:
MCA – 405 Elective –I (A) Java Programming & Technology
Remote Method Invocation
Remote Method Invocation
„Networking”.
CSI 4118 – UNIVERSITY OF OTTAWA
Distributed Computing
Java Remote Method Invocation
Java Chapter 5 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

第十一讲 分布式编程 Socket and RMI 李庆旭

2 本章内容提要  Socket TCP /IP protocol suit Socket A TCP Server and a TCP Client Supporting Multiple Concurrent Clients A UDP Server and a UDP Client  RMI What is RMI? How does RMI work? A RMI Server and a RMI Client How does RMI pass parameters? RMI vs. Socket  CORBA  Q&A

TCP/IP Protocol Suit 3

ISO/OSI Reference Model 4 Introduced in 1978, the ISO Open Systems Interconnection (OSI) Reference model describes networking as "a series of protocol layers with a specific set of functions allocated to each layer. Each layer offers specific services to higher layers while shielding these layers from the details of how the services are implemented.

TCP/IP Protocol Suit  TCP/IP protocol suit is a complete protocol stack with many protocols.  TCP: Transmission Control Protocol  IP: Internet Protocol  UDP: User Datagram Protocol  The first implementation of TCP/IP is in BSD Unix 4.x developed by 加州大学伯克利分校 5

TCP/IP and OSI Reference Model 6 TCP and UDP are at Transport layer IP is at Network layer

Socket 7

 BSD Socket is the programming interface of the TCP/IP protocol suit implemented in the BSD UNIX 4.x.  With BSD Socket, programmers can developed network programs using TCP, UDP and IP protocols.  Microsoft Windows also has a Socket implementation called WinSocket. 8

Socket (cont.)  Java Socket API is implemented with the underlying OS Socket API (e.g. BSD Socket andWinSocket). 9

TCP Port  To support many concurrent TCP connections, OS needs a way to recognize a specific TCP connection.  Each TCP connection is bound to a specific TCP port. A TCP port is an unsigned 16 bits integer.  TCP Ports from 0 to1024 are reserved.  Well-known TCP ports: HTTP(80),FTP(21), TELNET(23), etc. 10

UDP Port  To support many concurrent UDP connections, OS needs a way to recognize a specific UDP connection.  Each UDP connection is bound to a specific UDP port. A UDP port is an unsigned 16 bits integer.  UDP Ports from 0 to1024 are reserved.  Famous Protocols using UDP: TFTP, BOOTP, etc. 11

Server and Client  Normally, a server runs on a specific computer and has a socket that is bound to a specific port number. The server just waits, listening to the socket for a client to make a connection request.  On the client-side: The client knows the hostname of the machine on which the server is running and the port number on which the server is listening. 12

Server and Client (cont.)  First, the server listens on a specific TCP or UDP port. 13

Server and Client (cont.)  Then, a client tries to make a connection to the server. 14

Server and Client (cont.)  If everything goes well, the server accepts the connection. 15

A TCP Server and a TCP Client 16

A TCP Echo Server import java.io.*; import java.net.*; public class TCPEchoServer { public static void main(String[] args ) { try { ServerSocket s = new ServerSocket(7); Socket incoming = s.accept( ); BufferedReader in = new BufferedReader (new InputStreamReader(incoming.getInputStream())); PrintWriter out = new PrintWriter (incoming.getOutputStream(), true /* autoFlush */); 17

A TCP Echo Server (cont.) boolean done = false; while (!done) { String line = in.readLine(); if (line == null) done = true; else { System.out.println("Received: " + line); out.println("Echo: " + line); if (line.trim().equals("BYE")) done = true; } incoming.close(); } catch (Exception e) { System.out.println(e); } 18

A TCP ECHO Client import java.io.*; import java.net.*; public class TCPEchoClient { public static void main(String[] args) throws IOException { Socket echoSocket = null; PrintWriter out = null; BufferedReader in = null; try { echoSocket = new Socket("localhost", 7); out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader( echoSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: taranis."); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for " + "the connection to: taranis."); System.exit(1); } 19

A TCP ECHO Client (cont.) BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println(in.readLine()); } out.close(); in.close(); stdIn.close(); echoSocket.close(); } 20

Demo  Java TCPEchoServer  Java TCPEchoClient 21

Supporting Multiple Concurrent Clients 22

A Multi-threaded TCP Echo Server import java.io.*; import java.net.*; public class ThreadedTCPEchoServer { public static void main(String[] args ) { int i = 1; try { ServerSocket s = new ServerSocket(7); for (;;) { Socket incoming = s.accept( ); System.out.println("Spawning " + i); new ThreadedEchoHandler(incoming, i).start(); i++; } catch (Exception e) { System.out.println(e); } 23

A Multi-threaded TCP Echo Server (cont.) class ThreadedEchoHandler extends Thread { public ThreadedEchoHandler(Socket i, int c) { incoming = i; counter = c; } public void run() { try { BufferedReader in = new BufferedReader (new InputStreamReader(incoming.getInputStream())); PrintWriter out = new PrintWriter (incoming.getOutputStream(), true /* autoFlush */); 24

A Multi-threaded TCP Echo Server (cont.) boolean done = false; while (!done) { String str = in.readLine(); if (str == null) done = true; else { System.out.println("Received: " + str); out.println("Echo (" + counter + "): " + str); if (str.trim().equals("BYE")) done = true; } incoming.close(); } catch (Exception e) { System.out.println(e); } private Socket incoming; private int counter; } 25

Demo  Java ThreadedTCPEchoServer  Java TCPEchoClient 26

A UDP Server and a UDP Client 27

A UDP Echo Server import java.io.*; import java.net.*; public class UDPEchoServer { public static void main(String[] args ) { try { DatagramSocket socket = new DatagramSocket(7); boolean done = false; while (!done) { byte[] buf = new byte[256]; DatagramPacket packet = new DatagramPacket(buf, buf.length); socket.receive(packet); 28

A UDP Echo Server (cont.) String s= new String(buf); if( s.trim().equals("BYE")) done = true; else { System.out.println("Received: " + s); InetAddress address = packet.getAddress(); int port = packet.getPort(); packet = new DatagramPacket(buf, buf.length, address, port); socket.send(packet); } socket.close(); } catch (Exception e) { System.out.println(e); } 29

A UDP Echo Client import java.io.*; import java.net.*; public class UDPEchoClient { public static void main(String[] args) throws IOException { int port; InetAddress address; DatagramSocket socket = null; DatagramPacket packet; byte[] sendBuf = new byte[256]; if (args.length != 1) { System.out.println("Usage: java QuoteClient "); return; } socket = new DatagramSocket(); BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); String userInput; 30

A UDP Echo Client (cont.) while ((userInput = stdIn.readLine()) != null) { byte[] buf = userInput.getBytes(); address = InetAddress.getByName(args[0]); packet = new DatagramPacket(buf, buf.length, address, 7); socket.send(packet); packet = new DatagramPacket(buf, buf.length); socket.receive(packet); String received = new String(packet.getData(), 0, packet.getLength()); System.out.println("Received:" + received); } socket.close(); } 31

Demo  Java UDPEchoServer  Java UDPEchoClient localhost 32

What is RMI? 33

RMI  Local Method Invocation Normally, a java program only calls methods of classes whose.class files exist locally (i.e. within a same JVM). JVM dynamically load, verify, link and JIT.class files at run time.  Remote Method Invocation RMI enables java programs to call methods whose.class files exist remotely (i.e. Cross JVM). 34

How Does RMI Work? 35

How does RMI work? 36 Client Class Stub Class JVM Server Class Stub Class JVM TCP Socket over TCP/IP Network Legend Method CallMethod Return

How does RMI work? (cont.)  From the client’s perspective, calling a remote method is no different from calling a local method.  But, the client doesn’t call the remote method directly. Instead, it calls a local method in the Stub Class.  The sub class marshals method parameters, then sends a TCP packet over sockets. 37

How does RMI work? (cont.)  The stub class on the server listens on a TCP socket. After accepting the packet from the client, the stub class on the server side un- marshals method parameters then pass them to the actual server class.  The server class executes its method as if it is called by the stub class on the server side. After this method return, this stub class marshals the return value and all parameters, then send a return packet back to the client. 38

How does RMI work? (cont.)  Then, the stub class on the client receives the result packet and un-marshals it. After that, it returns back to the client class. 39

RMI Registry 40

RMI Registry  RMI Server registers its remote objects into the RMI Registry.  RMI Client looks up remote objects from the RMI Registry. 41

RMI Registry (cont.) 42 RMI Registry RMI Server RMI Client Object Store Registers remote objectsLooks up remote objects

A RMI Server and a RMI Client 43

The Interface Implemented by the Server import java.rmi.*; // All remote interfaces must derive from the // Remote interface and throw RemoteException. public interface Product extends Remote { String getDescription(String clientID) throws RemoteException; } 44

The Server Implementation Class import java.rmi.*; import java.rmi.server.*; public class ProductImpl extends UnicastRemoteObject implements Product { public ProductImpl(String n) throws RemoteException { name = n; } public String getDescription(String clientID) throws RemoteException { return "Client ID:" + clientID + ".I am a " + name + ". Buy me!"; } private String name; } 45

The RMI Server import java.rmi.*; import java.rmi.server.*; import sun.applet.*; public class ProductServer { public static void main(String args[]) { try { System.out.println ("Constructing server implementations..."); ProductImpl p1 = new ProductImpl("Blackwell Toaster"); ProductImpl p2 = new ProductImpl("ZapXpress Microwave Oven"); System.out.println ("Binding server implementations to registry..."); Naming.rebind("toaster", p1); Naming.rebind("microwave", p2); System.out.println ("Waiting for invocations from clients..."); } catch(Exception e) { System.out.println("Error: " + e); } 46

The RMI Client import java.rmi.*; import java.rmi.server.*; public class ProductClient { public static void main(String[] args) { System.setSecurityManager(new RMISecurityManager()); String url = "rmi://localhost/"; // change to "rmi://yourserver.com/" // when server runs on remote machine // yourserver.com try { Product c1 = (Product)Naming.lookup(url + "toaster"); Product c2 = (Product)Naming.lookup(url + "microwave"); System.out.println(c1.getDescription("client 1")); System.out.println(c2.getDescription("client 2")); } catch(Exception e) { System.out.println("Error " + e); } System.exit(0); } 47

Compile  javac *.java  rmic ProductImpl 48

OFF start rmiregistry REM start the server start java ProductServer REM wait the server to start Pause REM start the client java -Djava.security.policy=client.policy ProductClient 49

Deploy  Client Product.class (Interface class) ProductImpl_Stub.class (Stub class) ProductClient.class Client.policy  Server Product.class (Interface class) ProductImpl_Stub.class(Stub class) ProductImpl.class (Server Implementation Class) ProductServer.class (Server startup class) 50

How does RMI pass parameters? 51

How does RMI pass parameters?  The stub class on the caller side marshals parameters. Marshaling means serializing parameters into a byte stream.  The stub class on the callee side un-marshals parameters. Un-marshaling means de- serializing parameters from the byte stream.  So, all the parameters of a remote method must be Serializable.Serializable 52

RMI vs. Socket 53

RMI vs. Socket  RMI Simple to use But, can only connect to Java Programs. For example, cannot connect to C++ programs.  Socket Difficult to use Can connect to programs written in other languages only if it supports socket. So, a java client can connect to a C++ server via socket. 54

55 Q&A  Thanks !