Network Communication

Similar documents
Remote Method Invocation

Java Network. Slides prepared by : Farzana Rahman

Creating a Simple, Multithreaded Chat System with Java

Transport layer protocols. Message destination: Socket +Port. Asynchronous vs. Synchronous. Operations of Request-Reply. Sockets

When the transport layer tries to establish a connection with the server, it is blocked by the firewall. When this happens, the RMI transport layer

Network Programming using sockets

Programmation RMI Sécurisée

Remote Method Invocation in JAVA

! "# $%&'( ) * ).) "%&' 1* ( %&' ! "%&'2 (! ""$ 1! ""3($

Remote Method Invocation (RMI)

Division of Informatics, University of Edinburgh

String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivepacket.getaddress(); int port = receivepacket.

Network/Socket Programming in Java. Rajkumar Buyya

The difference between TCP/IP, UDP/IP and Multicast sockets. How servers and clients communicate over sockets

NETWORKING FEATURES OF THE JAVA PROGRAMMING LANGUAGE

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

Building a Multi-Threaded Web Server

Application Development with TCP/IP. Brian S. Mitchell Drexel University

Socket Programming. Announcement. Lectures moved to

Limi Kalita / (IJCSIT) International Journal of Computer Science and Information Technologies, Vol. 5 (3), 2014, Socket Programming

Learning Outcomes. Networking. Sockets. TCP/IP Networks. Hostnames and DNS TCP/IP

SSC - Communication and Networking Java Socket Programming (II)

Data Communication & Networks G

Socket-based Network Communication in J2SE and J2ME

Computer Networks/DV2 Lab

Lesson: All About Sockets

Java Programming: Sockets in Java

NETWORK PROGRAMMING IN JAVA USING SOCKETS

DNS: Domain Names. DNS: Domain Name System. DNS: Root name servers. DNS name servers

Assignment 4 Solutions

Socket Programming in Java

Network Programming TDC 561

Socket = an interface connection between two (dissimilar) pipes. OS provides this API to connect applications to networks. home.comcast.

Java Network Programming. The java.net package contains the Socket class. This class speaks TCP (connection-oriented protocol).

Lecture 7: Java RMI. CS178: Programming Parallel and Distributed Systems. February 14, 2001 Steven P. Reiss

Agenda. Network Programming and Java Sockets. Introduction. Internet Applications Serving Local and Remote Users

The Java Series Introduction to Java RMI and CORBA. The Java Series. Java RMI and CORBA Raul RAMOS / CERN-IT User Support Slide 1

ExempleRMI.java. // Fichier de defintion des droits et proprietes // System.setProperty("java.security.policy","../server.java.

Lecture 28: Internet Protocols

Transport Layer Protocols

Communicating with a Barco projector over network. Technical note

LESSON Networking Fundamentals. Understand TCP/IP

Socket programming. Socket Programming. Languages and Platforms. Sockets. Rohan Murty Hitesh Ballani. Last Modified: 2/8/2004 8:30:45 AM

Objectives of Lecture. Network Architecture. Protocols. Contents

An Android-based Instant Message Application

Agenda. Distributed System Structures. Why Distributed Systems? Motivation

Network-Oriented Software Development. Course: CSc4360/CSc6360 Instructor: Dr. Beyah Sessions: M-W, 3:00 4:40pm Lecture 2

TP1 : Correction. Rappels : Stream, Thread et Socket TCP

Socket programming. Complement for the programming assignment INFO-0010

Transparent Redirection of Network Sockets 1

Report of the case study in Sistemi Distribuiti A simple Java RMI application

Overview. Securing TCP/IP. Introduction to TCP/IP (cont d) Introduction to TCP/IP

Network-based Applications. Pavani Diwanji David Brown JavaSoft

Introduction to Java. Module 12: Networking (Java Sockets) Prepared by Costantinos Costa for EPL 233. ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 1

Basic Networking Concepts. 1. Introduction 2. Protocols 3. Protocol Layers 4. Network Interconnection/Internet

Abhijit A. Sawant, Dr. B. B. Meshram Department of Computer Technology, Veermata Jijabai Technological Institute

Transparent Redirection of Network Sockets 1

Session NM059. TCP/IP Programming on VMS. Geoff Bryant Process Software

Remote Method Invocation

Chapter 11. User Datagram Protocol (UDP)

Intranet, Extranet, Firewall

Socket Programming. A er learning the contents of this chapter, the reader will be able to:

Introduction to Computer Networks

Internet Concepts. What is a Network?

IT6503 WEB PROGRAMMING. Unit-I

Lecture 17. Process Management. Process Management. Process Management. Inter-Process Communication. Inter-Process Communication

Mail User Agent Project

IP Network Layer. Datagram ID FLAG Fragment Offset. IP Datagrams. IP Addresses. IP Addresses. CSCE 515: Computer Network Programming TCP/IP

JAVA Program For Processing SMS Messages

TITLE NETWORK PROGRAMMING (SECURED CLIENT-SERVER CHAT APPLICATION) A PROJECT REPORT PRESENTED ACHILE UGBEDE JOHN CE/2007/165

Cross-platform TCP/IP Socket Programming in REXX

Network Models OSI vs. TCP/IP

Understanding TCP/IP. Introduction. What is an Architectural Model? APPENDIX

First Workshop on Open Source and Internet Technology for Scientific Environment: with case studies from Environmental Monitoring

Chapter 3. Internet Applications and Network Programming

EXPLORER. TFT Filter CONFIGURATION

Internet Protocol: IP packet headers. vendredi 18 octobre 13

COMMMUNICATING COOPERATING PROCESSES

Lecture 2-ter. 2. A communication example Managing a HTTP v1.0 connection. G.Bianchi, G.Neglia, V.Mancuso

Introduction to Socket Programming Part I : TCP Clients, Servers; Host information

Protocols and Architecture. Protocol Architecture.

The TCP/IP Reference Model

Transport and Network Layer

The OSI and TCP/IP Models. Lesson 2

Introduction To Computer Networking

Java Programming Unit 10. Stock Price Quotes with URL, Sockets, and RMI

CONNECTING WINDOWS XP PROFESSIONAL TO A NETWORK

Network Simulation Traffic, Paths and Impairment

Network Programming with Sockets. Process Management in UNIX

Lecture (02) Networking Model (TCP/IP) Networking Standard (OSI) (I)

Ethernet. Ethernet. Network Devices

The OSI model has seven layers. The principles that were applied to arrive at the seven layers can be briefly summarized as follows:

ICOM : Computer Networks Chapter 6: The Transport Layer. By Dr Yi Qian Department of Electronic and Computer Engineering Fall 2006 UPRM

Slide 1 Introduction cnds@napier 1 Lecture 6 (Network Layer)

Detailed Table of Contents

Windows Sockets Network Programming

1. The Web: HTTP; file transfer: FTP; remote login: Telnet; Network News: NNTP; SMTP.

How To Understand The Internet Of S (Netware)

Transcription:

Network Communication

Outline Sockets Datagrams TCP/IP Client-Server model

OSI Model

Sockets Endpoint for bidirectional communication between two machines. To connect with each other, each of the client and the server binds a socket to its end for reading and writing: read from socket input and write to socket output Characterized by: Local address: IP: identifies computer Port number: identifies application Protocol: ex: TCP, UDP

Socket types Datagram sockets connectionless sockets use User Datagram Protocol (UDP). Stream sockets connection-oriented sockets use Transmission Control Protocol (TCP) Raw sockets available in routers

Datagrams The UDP protocol provides a mode of network communication whereby datagrams are sent over the network. java.net.datagramsocket used for the connection. A datagram s arrival, arrival time and order of arrival is not guaranteed.

Communicating through datagrams Create a socket on local machine to send/receive datagrams: DatagramSocket To send a message: build a packet containing the data: DatagramPacket add the destination IP address: InetAdress send it using DatagramSocket To receive a message: build a packet with an empty buffer wait to receive a message using DatagramSocket

import java.net.*; UDP Example class GetDate { final static int PORT_DAYTIME = 13; public static void main(string[] args) throws Exception { DatagramSocket dgsocket = new DatagramSocket(); InetAddress destination = InetAddress.getByName( server.com");; DatagramPacket datagram; byte[] msg = new byte[256]; datagram = new DatagramPacket(msg, msg.length, destination, PORT_DAYTIME); dgsocket.send(datagram); datagram = new DatagramPacket(msg, msg.length); dgsocket.receive(datagram); String received = new String(datagram.getData()); System.out.println("Time of machine:" + received); dgsocket.close();

TCP / IP Internet Protocol (IP) Rules for moving bits from A to B. Divide data into packets (header bits to say where to go, data bits) and transmit each individually, possibly along different paths. No guarantee packets arrive in order, or even arrive. Transmission Control Protocol (TCP) Rules to provide reliable communication between two computers. Packets arrive, and they arrive in order. resend over IP until recipient acknowledges

Protocols Many higher layer application protocols use TCP/IP. Used by http, smtp, telnet, ftp. Ex: HyperText Transfer Protocol (HTTP). Set of rules for transferring files (text, graphics, video). Web-server waits for connection requests. Browser establishes connection with Web-server Browser issues GET and POST commands. Web-server responds with header and body.

Client-server model Client: creates socket to communicate with specified server. Socket Server: creates one socket to listen for connection requests creates other sockets to communicate with each client. ServerSocket

Establish a simple server 1. Create a ServerSocket object: ServerSocket server = new ServerSocket(port, queuelength); 2. The server listens indefinitely (or blocks) for an attempt by a client to connect: Socket connection = server.accept(); 3. Get the OutputStream and InputStream objects that enable the server to communicate with the client by sending and receiving bytes: InputStream input = connection.getinputstream(); OutputStream output = connection.getoutputstream(); 4. Processing phase: the server and the client communicate via the InputStream and the OutputStream objects 5. After the communication completes, the server closes the connection by invoking close() on the Socket and the corresponding streams

Establish a simple client 1. Create a Socket object: Socket connection = new Socket (serveraddress, port); 2. Get the OutputStream and InputStream of the Socket. The server and the client must send and receive the data in the same format 3. Processing phase: the server and the client communicate via the InputStream and the OutputStream objects 4. After the communication completes, the client closes the connection.

Multi-threaded server Server listens for connection requests in one thread. Server handles communication with each client in a separate thread. Makes server scalable can accept requests, independent of speed in handling them.

Server example class Serveur { public static void main(string[] arg) { int portecoute = 10302; ServerSocket standardiste; Socket socket; try { standardiste = new ServerSocket(portEcoute); while(true) { socket = standardiste.accept(); new Service(socket); catch(ioexception exc) { System.out.println("probleme de connexion");

Server example class Service extends Thread { Socket socket; BufferedReader entree; PrintStream sortie; Service(Socket socket) { this.socket = socket; try { entree = new BufferedReader(new InputStreamReader(socket.getInputStream())); sortie = new PrintStream(socket.getOutputStream()); this.start(); catch(ioexception exc) { try { socket.close(); catch(ioexception e){ public void run() { String texte; try { while(!(texte = entree.readline()).equals("end")) //do some processing with texte sortie.println( reply to client"); sortie.close(); entree.close(); socket.close(); catch(ioexception e) {

Client example class Client { public static void main(string[] arg) { int portecouteserveur = 10302; BufferedReader lecteurfichier; BufferedReader entree; PrintStream sortie; String ligne; Socket socket; try { socket = new Socket(arg[1], portecouteserveur); lecteurfichier = new BufferedReader(new FileReader(arg[0])); entree = new BufferedReader(new InputStreamReader(socket.getInputStream())); sortie = new PrintStream(socket.getOutputStream()); while ((ligne = lecteurfichier.readline())!= null) sortie.println(ligne); sortie.println("end"); System.out.println(entree.readLine()); sortie.close(); entree.close();socket.close(); catch(filenotfoundexception exc){system.out.println( Fich.introuvable"); catch(unknownhostexception exc){system.out.println( Dest.inconnu"); catch(ioexception exc){system.out.println("probleme d'entree-sortie");

Other types of communication Peer-to-peer Multicast RMI

Remote Method Invocation RMI allows an object running in one Java Virtual Machine (VM) to invoke methods on an object running in another Java VM Application RMI java.net RMI defines a high-level protocol and API TCP/IP stack network

Distributed objects Simple idea objects existing on one machine (server) may be accessed from another machine through regular method call Eliminates need to marshal and unmarshal data sent over sockets Underlying socket code still exists, but is not programmed by user

Distributed objects Client Host Java Virtual Machine Server Host Java Virtual Machine Client Object Remote Object Stub Skeleton

Remote interface Remote Interface implements implements Client Stub Skeleton Remote Object (Server)

Additional readings Custom networking http://docs.oracle.com/javase/tutorial/ networking/index.html JavaTM Programming Language Basics, Socket Communications http://www.oracle.com/technetwork/java/ socket-140484.htmlsocket.html

Appendix

RMI steps Four files need to be created: An interface listing the methods in the implementation class which you want to make remote (FooInterface.java) The implementation class (Foo.java) A server class, which creates one or more implementation objects and posts them to the registry (FooServer.java) A client that invokes the remote object methods (FooClient.java)

The remote object Interface file (e.g. FooInterface.java) must extend java.rmi.remote All methods in interface must throw java.rmi.remoteexception Implementation file (e.g Foo.java) must extend java.rmi.server.unicastremoteobject and implement FooInterface Explicitly include a call to super() as first line in constructor

The server Server class (e.g. FooServer.java) Create a new object instance Call java.rmi.naming.bind( ) to register the object with the naming service contains String name associated with bound object

The client Calls Naming.lookup() to get remote object reference: uses cast to interface type Handles the RMI exceptions Once the remote object reference obtained, handled as regular object

HelloInterface.java import java.rmi.*; public interface HelloInterface extends Remote { public String say(string msg) throws RemoteException;

Hello.java import java.rmi.*; import java.rmi.server.*; public class Hello extends UnicastRemoteObject implements HelloInterface { private String message; public Hello(String msg) throws RemoteException { message = msg; public String say(string m) throws RemoteException { // return input message - reversing input and suffixing // our standard message return new StringBuffer(m).reverse().toString() + "\n" + message;

HelloServer.java import java.rmi.*; import java.rmi.server.*; public class HelloServer { public static void main(string args[]) { // Create and install a security manager if (System.getSecurityManager() == null) System.setSecurityManager(new RMISecurityManager()); try { Naming.rebind("Hello", new Hello("Hello, world!")); System.out.println("server is running..."); catch (Exception e) { System.out.println("Hello server failed:" + e.getmessage());

HelloClient.java import java.rmi.*; public class HelloClient { public static void main(string args[]) { String path = //server.com/hello"; try { if (args.length < 1) System.out.println( usage: HelloClient <host:port> <string>..."); else path = "//" + args[0] + "/Hello"; HelloInterface hello = (HelloInterface) Naming.lookup(path); for (int i = 0; i < args.length; ++i) System.out.println(hello.say(args[i])); catch(exception e) { System.out.println("HelloClient exception: " + e);

Deploying the application Write and compile the 4 Java files Generate Stub and Skeleton class files from the implementation classes: rmic Hello Start the rmi registry (at server): rmiregistry Start the server with or without the security policy: java -D java.security.policy=policy HelloServer Run the client: java HelloClient testing

Deploying the application Java Virtual Machine Client 2 3 Stub 4 Java Virtual Machine 5 Skeleton Remote Object Server 1 Client obtains a remote reference from the registry, and a stub is created Registry Bob Java Virtual Machine 1 Server creates and registers remote object