Network. Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark

Similar documents
Sockets and RMI. CS151 Chris Pollett Dec. 5, 2005.

JAVA RMI. Remote Method Invocation

COMP 213. Advanced Object-oriented Programming. Lecture 20. Network Programming

Previous lecture: threads G51PRG: Introduction to Programming Second semester Lecture 12 URL

Object-Oriented Programming in Java

CS 2113 Software Engineering

Distributed Systems. 02r. Java RMI Programming Tutorial. Paul Krzyzanowski TA: Long Zhao Rutgers University Fall 2017

Network Programming. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Chapter 4: Processes. Process Concept. Process State

Lecture 3: Socket Programming

Contents. Java RMI. Java RMI. Java RMI system elements. Example application processes/machines Client machine Process/Application A

URL Kullanımı Get URL

Distributed Programming in Java. Distribution (2)

Java Networking (sockets)

JAVA - NETWORKING (SOCKET PROGRAMMING)

CC755: Distributed and Parallel Systems

Networking Basics. network communication.

CSci Introduction to Distributed Systems. Communication: RPC In Practice

Object Interaction. Object Interaction. Introduction. Object Interaction vs. RPCs (2)

Distributed Systems COMP 212. Lecture 10 Othon Michail

CS September 2017

Topic 10: Network Programming

public static void main(string[] args) throws IOException { sock = new Socket(args[0], Integer.parseInt(args[1]));

03 Remote invocation. Request-reply RPC. Coulouris 5 Birrel_Nelson_84.pdf RMI

TCP connections. Fundamentals of Internet Connections Objectives. Connect to an Echo port. java.net.socket

Network Programming 1

Networking Code CSCI 201 Principles of Software Development

What is Serialization?

Distributed Objects SPL/ SPL 201 / 0 1

RMI Case Study. A Typical RMI Application

Generic architecture

Remote Objects and RMI

JAC444 - Lecture 11. Remote Method Invocation Segment 2 - Develop RMI Application. Jordan Anastasiade Java Programming Language Course

Unit 9: Network Programming

Remote Method Invocation

CPSC 441 Tutorial TCP Server. Department of Computer Science University of Calgary

Pieter van den Hombergh Richard van den Ham. March 17, 2018

FILE I/O IN JAVA. Prof. Chris Jermaine

CS 351 Design of Large Programs Sockets Example

IT101. File Input and Output

CS 5523 Operating Systems: Remote Objects and RMI

How are classes loaded into the Java Virtual Machine (JVM)? from the local file system (CLASSPATH). by an instance of ClassLoader

I/O Streams. Object-oriented programming

CSCD 330 Network Programming Spring 2018

Socket 101 Excerpt from Network Programming

IBD Intergiciels et Bases de Données

Remote Method Invocation

RMI Example RMI. CmpE 473 Internet Programming RMI

Internet Technology 2/7/2013

Distributed Systems Recitation 2. Tamim Jabban

Java Technologies. Lecture VII. Valdas Rapševičius. Vilnius University Faculty of Mathematics and Informatics

Distributed Systems. Distributed Object Systems 2 Java RMI. Java RMI. Example. Applet continued. Applet. slides2.pdf Sep 9,

Distributed Objects. Remote Method Invokation

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003

The UNIVERSITY of EDINBURGH. SCHOOL of INFORMATICS. CS4/MSc. Distributed Systems. Björn Franke. Room 2414

55:182/22C:182. Distributed Application Frameworks Java RMI, CORBA, Web Services (SOAP)

CS555: Distributed Systems [Fall 2017] Dept. Of Computer Science, Colorado State University

Distributed Programming - Sockets

Written by: Dave Matuszek

Outline. EEC-681/781 Distributed Computing Systems. The OSI Network Architecture. Inter-Process Communications (IPC) Lecture 4

Desarrollo de Aplicaciones en Red RMI. Introduction. Considerations. Considerations. RMI architecture

Chapter 4 Remote Procedure Calls and Distributed Transactions

Remote Procedure Call

SUMMARY INTRODUCTION REMOTE METHOD INVOCATION

Communication and Distributed Processing

JAVA RMI Java, summer semester

pre-emptive non pre-emptive

CN208 Introduction to Computer Programming

RMI. Remote Method Invocation. 16-Dec-16

Java. Java. Java. Java. java. smalltalk. java. java virtual machine. java. C++ virtual. interface C++ Operator overloading Template C++ test.

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Grid Computing. Java Remote Method Invocation (RMI) RMI Application. Grid Computing Fall 2006 Paul A. Farrell 9/5/2006

Distributed Systems Recitation 2. Tamim Jabban

protocols September 15,

Java Programming Language Advance Feature

Advanced Java Programming. Networking

Distributed Computing

CS 355. Computer Networking. Wei Lu, Ph.D., P.Eng.

Input from Files. Buffered Reader

Java.net Package and Classes(Url, UrlConnection, HttpUrlConnection)

1 interface TemperatureSensor extends java.rmi.remote 2 { 3 public double gettemperature() throws java.rmi.remoteexception; 4 public void

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

Remote Method Invocation in Java

Verteilte Systeme (Distributed Systems)

Distributed Systems. 5. Remote Method Invocation

presentation DAD Distributed Applications Development Cristian Toma

Agent and Object Technology Lab Dipartimento di Ingegneria dell Informazione Università degli Studi di Parma. Distributed and Agent Systems RMI

Introduction to Sockets

CS 251 Intermediate Programming Java I/O Streams

RMI (Remote Method Invocation) Over the year, there have been 3 different approaches to application development:

Component-Based Software Engineering

Remote Method Invocation

Chapter 15: Distributed Communication. Sockets Remote Procedure Calls (RPCs) Remote Method Invocation (RMI) CORBA Object Registration

Distributed Systems COMP 212. Lecture 8 Othon Michail

Network Programming Benoît Garbinato

JOSE LUIS JUAREZ VIVEROS com) has a. non-transferable license to use this Student Guide

Getting Started in Java. Bill Pugh Dept. of Computer Science Univ. of Maryland, College Park

Communication and Distributed Processing

Outline. Chapter 4 Remote Procedure Calls and Distributed Transactions. Remote Procedure Call. Distributed Transaction Processing.

Week 13 Lab - Exploring Connections & Remote Execution

Transcription:

Network Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark jbb@ase.au.dk

Outline Socket programming If we have the time: Remote method invocation (RMI) 2

Socket Programming Sockets Ends points of two-way communications, i.e., logical connections between hosts Can be used to send and receive data Supported by most languages and platforms Server vs. client sockets sockets (end points) logical connection 3

Socket Programming (Cont.) Server sockets Wait for requests to come in over the network Implemented by java.net.serversocket class Client sockets Used to send and receive data Can be thought of as a pair of input and output streams Implemented by java.net.socket class 4

Server vs. Client Sockets Server socket: waiting for connection requests Client socket: transmission of data server socket connection request client socket 5

Server Sockets Creating and using server sockets Constructors ServerSocket(int port) ServerSocket(int port, int backlog) Methods accept() close() Description Waits for a connection request and returns a Socket Stops waiting for requests from clients 6

Server Sockets (Cont.) Usage pattern try { ServerSocket server = new ServerSocket(8888); while (true) { Socket incoming = server.accept(); // obtain a client socket // handle client request by reading from and writing to the socket catch (IOException e) { // handle exception on creating a server socket 7

Client Sockets Creating client sockets On the client side, Socket(String host, int port) On the server side, accept() of ServerSocket Using client sockets Methods Description getinputstream() Returns an InputStream for receiving data getoutputstream() Returns an OutputStream to send data close() Closes the socket connection 8

Client Sockets (Cont.) Usage pattern try { Socket s = new Socket( host name, 8888); PrintWriter out = new PrinterWriter( new OutputStreamWriter(s.getOutputStream())); BufferedReader in = new BufferedReader( new InputStreamReader(s.getInputStream())); // send and receive data by using out and in in.close(); out.close(); s.close(); catch (IOException e) { // handle exception 9

Example -- A Simple Echo Server import java.io.*; import java.net.*; public class EchoServer { public static void main(string[] args) { try { ServerSocket server = new ServerSocket(8008); while (true) { Socket s = server.accept(); BufferedReader in = new BufferedReader( new InputStreamReader( s.getinputstream())); PrintWriter out = new PrintWriter( new OutputStreamWriter( s.getoutputstream())); <<handle client by using in and out next slide>> s.close(); catch (Exception e) { e.printstacktrace(); 10

Echo Server (Cont.) <<handle client by using in and out>>= out.print( Hello! This is the Java EchoSever. ); out.println( Enter BYE to exit. ); out.flush(); String str = null; while ((str = in.readline())!= null) { System.out.println( Received: + str); out.println( Echo: + str); out.flush(); if (str.trim().equals( BYE )) { break; 11

Testing Echo Server Testing with telnet client. 12

Turning on telnet in windows Advanced Java 13

A Simple Echo Client import java.io.*; import java.net.*; public class EchoClient { public static void main(string[] args) { String host = args.length > 0? host = args[0] : localhost ; try { Socket socket = new Socket(host, 8008); BufferedReader in = new BufferedReader( new InputStreamReader( socket.getinputstream())); PrintWriter out = new PrintWriter(new OutputStreamWriter( socket.getoutputstream())); <<send and receive data by using in and out next slide>> socket.close(); catch (Exception e) { e.printstacktrace(); 14

Echo Client (Cont.) <<send and receive data by using in and out>>= // send data to server for (int i = 1; i <= 10; i++) { System.out.println( Sending: line + i); out.println( line + i); out.flush(); out.println( BYE ); out.flush(); // receive data from server String str = null; while ((str = in.readline())!= null) { System.out.println(str); 15

Testing Echo Client 16

Echo Server Revisited How to support multiple clients simultaneously? import java.io.*; import java.net.*; public class MultiEchoServer { public static void main(string[] args) { try { ServerSocket server = new ServerSocket(8008); while (true) { Socket s = server.accept(); new ClientHandler(s).start(); catch (Exception e) { e.printstacktrace(); <<class ClientHandler>> 17

Multi Echo Server (Cont.) <<class ClientHandler>>= private static class ClientHandler extends Thread { private Socket sock; public ClientHandler(Socket sock) { this.sock = sock; public void run() { BufferedReader in = new BufferedReader(new InputStreamReader( sock.getinputstream())); PrintWriter out = new PrintWriter(new OutputStreamWriter( sock.getoutputstream())); out.println( Hello! This is the Java EchoSever.\n Enter BYE to exit. ); out.flush(); String str = null; while ((str = in.readline())!= null) { out.println( Echo: + str); out.flush(); if (str.trim().equals( BYE )) { break; 18

Outline Socket programming Remote method invocation (RMI) 21

Remote Method Invocation Why RMI? In socket programming, programmers have to make explicit connections between clients and servers and manage data transmission. Thus, it s hard and error-prone to write socket programs. Can the connection and data transmission be managed by JVM? 22

What s RMI? Distributed programming model to allow objects residing on different hosts (remote objects) to be manipulated as if they were all on the same host (local objects) RMI architecture Client call return Server 1 6 3 4 Stub JVM 2 5 Skeleton JVM 23

Local vs. Remote Objects Local objects Objects accessible only within the local hosts Remote objects Objects accessible from remote hosts Instances of classes that implements a marker interface java.rmi.remote Property of remote objects Similar to local objects (arguments, downcasting, instanceof, etc) Clients of remote objects interact with stubs Passing arguments and results for RMI calls Call by value for local objects (through serialization and deserialization) Call by reference for remote objects 24

Locating Remote Objects RMI registry Directory service mapping RMI servers (or objects) to their names Server: register itself to make it available to remote clients Client: locate a server by looking up an RMI registry with a URL protocol rmi, e.g., rmi://host:port/name The programming interface by the class java.rmi.naming Method bind(name, obj) rebind(name, obj) unbind(name) lookup(url) list(url) Description Bind obj to name Bind obj to name even if already bound Remove the binding Return object bound to url Return a list of all bindings 25

Writing RMI Programs 1. Define a remote interface, e.g., public interface Service extends java.rmi.remote { public void dosomething( ) throws java.rmi.remoteexception; // 2. Define a service implementation class, e.g., public class ServiceProvider extends java.rmi.server.unicastremoteobject implements Service { public void dosomething( ) throws java.rmi.remoteexception { // // 26 CS 333 1

Writing RMI Programs (Cont.) 3. Create a server instance and register to an RMI registry, e.g., Service server = new ServiceProvider( ); java.rmi.naming.bind(name, server); 4. Generate the stub and skeleton classes by using the RMI compiler (rmic), e.g., % rmic ServiceProvider The command produces: ServiceProvider_Stub.class and ServiceProvider_Skel.class 27 CS 333 1

Writing RMI Programs (Cont.) 5. Write a client program, e.g., java.rmi.remote obj = java.rmi.naming.lookup(name); Service server = (Service) obj; server.dosomething( ); // RMI call 28 CS 333 1

Example -- A Simple Time Server 1. Create an interface. (in this case, TimeService.java). 2. Create a class that implements the interface. (in this case, TimeServer.java). 3. Create a client that connects to the server object using Naming.lookup() (in this case, TimeClient.java). 4. Compile these classes. 5. Run the RMI interface compiler on the.class file of the implementation class (in this case, you'd say "rmic TimeServer"). 6. Start the RMI registry (on Windows say "start rmiregistry"). 7. Start the server class ("start java TimeServer"). 8. Run the client program ("java TimeClient"). 29

A Simple TimeServer Remote interface, TimeService public interface TimeService extends java.rmi.remote { java.util.date gettime() throws java.rmi.remoteexception; Server and client classes 30

A Server Class, TimeServer import java.rmi.*; import java.util.*; public class TimeServer extends java.rmi.server.unicastremoteobject implements TimeService { public TimeServer() throws RemoteException { public Date gettime() { return Calendar.getInstance().getTime(); public static void main(string [] args) { try { TimeServer server = new TimeServer(); Naming.rebind("TimeServer", server); catch (Exception e) { e.printstacktrace(); 31

A Client Class, TimeClient import java.rmi.*; improt java.util.*; public class TimeClient { public static void main(string [] args) { try { TimeService server = (TimeService) Naming.lookup( "rmi://localhost/timeserver"); System.out.println(server.getTime()); catch (Exception e) { e.printstacktrace(); 32

Compiling and Running (UNIX) 1. Compile the server and client programs, e.g., % javac TimeServer.java TimeClient.java TimeService.java 2. Generates the stubs and skeletons, e.g., % rmic TimeServer 3. Start the RMI registry on the server host, e.g., % rmiregistry & 4. Run the server on the server host, e.g., % java TimeServer & 5. Runt the client on the client host, e.g., % java TimeClient 33

Create stubs Start registry Start server Start client Compile and run (Win) Advanced Java 34

Serialization What is it? Process of transforming an object into a stream of bytes; the reverse process is called deserialization. Allows objects to be saved to files or sent to remote hosts over a network (e.g., arguments to RMI calls) How to make objects serializable? By implementing the marker interface java.io.serializable A default implementation for (de) serialization is automatically provided. Can customize the process by implementing readobject() and writeobject() methods: private void writeobject(java.io.objectoutputstream out) throws IOException; private void readobject(java.io.objectinputstream in) throws IOException, ClassNotFoundException; 35

Example Make the following class Student serializable public class Student { private String name; private int score; //@ private invariant 0 <= score && score <= 100; private char grade; //@ private invariant (* grade is one of A,, F *); // Answer 1: public class Student implements Serializable { // 36

Example (Cont.) Answer 2: public class Student implements Serializable { // private void writeobject(java.io.objectoutputstream out) throws IOException { out.writeutf(name); out.writeint(score); out.writechar(grade); private void readobject(java.io.objectinputstream in) throws IOException, ClassNotFoundException { name = in.readutf(); score = in.readint(); grade = in.readchar(); 37

Example (Cont.) Answer 3: public class Student implements Serializable { // private void writeobject(java.io.objectoutputstream out) throws IOException { out.writeutf(name); out.writeint(score); private void readobject(java.io.objectinputstream in) throws IOException, ClassNotFoundException { name = in.readutf(); score = in.readint(); grade = calculategrade(score); private char calculategrade(int score) { /* */ 38

Using Serialization Serializaing objects ObjectOutputStream out = new ObjectOutputStream(/* */); Student s = new Student(/* */); out.writeobject(s); // Deserializing objects ObjectInputStream in = new ObjectInputStream(/* */); Object obj = in.readobject(); Student s = (Student) obj; // 39