43
Networking in Java Rodrigo García García [email protected] http://lgl.epfl.ch Software Engineering Lab Swiss Federal Institute of Technology Lausanne Switzerland 5.2.2004

networking in java

Embed Size (px)

Citation preview

Page 1: networking in java

Networking in JavaRodrigo García Garcí[email protected]

http://lgl.epfl.ch

Software Engineering LabSwiss Federal Institute of Technology Lausanne

Switzerland

5.2.2004

Page 2: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 2 -

Introduction to Networking

ApplicationApplication

PresentationPresentation

SessionSession

TransportTransport

NetworkNetwork

LinkLink

PhysicalPhysical

OSI Model Internet Protocols

HTTP, FTP…HTTP, FTP…

TCP, UDPTCP, UDP

IP, ICMPIP, ICMP

HDLC, LLC…HDLC, LLC…

RJ45, RS232…RJ45, RS232…

RPC, RMI…

Connectors, volts, wires

Error/flow control

Routing

Synchro, recovery

Data transformation

Facilities

Reliability, multiplex

SSL, XDR…

Page 3: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 3 -

Transport protocolsThere are two widely used transport Internet protocols:

l TCP (Transmission Control Protocol)• Connection oriented.• Reliable

• Error and flow control.• Order guaranteed.• Delivery guaranteed.

lUDP (User Datagram Protocol)• Connectionless. Packet (datagram) based.• Not reliable

• Losses, duplicates…• Order of arrival not guaranteed.• Delivery not guaranteed.

Page 4: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 4 -

PortslOne machine one address, but several applications:

• How to distinguish different connections?

lA Port is a 16-bit number that identifies an application in a remote machine.

lStandardized ports reserved for some applications:• HTTP (port 80)• Echo (port 7)• Daytime (port 13)• …

lUse ports 1024-65,535 for your own applications• Administrator privileges are required for working with ports in

the range 0-1023.

Page 5: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 5 -

Introduction to Sockets in JavalA Socket is an end point of a communication link

• Determined by local and remote host address and port.• Stream based, duplex communication over TCP.

lHow to program with sockets:• Server side

• Create a server socket and bind it to a “well-known” port.• Listen for incoming clients.• Upon acceptance, create a normal socket for each client.• Get output and input streams from the socket.• Communicate with the client through the streams from the socket.• Close the socket.

• Client side• Create a normal socket in any available local port.• Connect it to the remote server (in Java, this is done at creation).• Communicate with the server through streams from the socket.• Close the socket.

Page 6: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 6 -

Socket Programming

Server ClientKnownport Server

Socket

SocketSocket Port

connect

accept

Duplex communication

Page 7: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 7 -

Sockets in Java: Server Side (I)import java.net.*;

import java.io.*;

import java.util.Date;

public class DateServer {

public static void main(String[] args) throws IOException {

ServerSocket serverSocket = null;

try {

serverSocket = new ServerSocket(1313);

} catch (IOException e) {

System.err.println("Could not listen on port: 1313.");

System.exit(1);

}

Socket clientSocket = null;

try {

clientSocket = serverSocket.accept();

} catch (IOException e) {

System.err.println("Accept failed.");

System.exit(1);

}

...

1.- Import network, input/output and application dependent classes.

2.- Create a server socket in a “well-known” port.

3.- Create a new client socket upon acceptance.

Page 8: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 8 -

Sockets in Java: Server Side (II)...

ObjectOutputStream dateOut = null;

dateOut = new ObjectOutputStream

(clientSocket.getOutputStream());

dateOut.writeObject(new Date());

dateOut.close();

clientSocket.close();

serverSocket.close();

}

}

4.- Get a stream from the socket andsend answer to the client.

5.- Close streams and sockets used.

- Note: Full objects can be sent through the streams, but they must be Serializable.

Page 9: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 9 -

Sockets in Java: Client Side (I)import java.io.*;

import java.net.*;

import java.util.Date;

public class DateClient {

public static void main(String[] args) throws IOException {

Socket dateSocket = null;

ObjectInputStream dateIn = null;

try {

dateSocket = new Socket("localhost", 1313);

dateIn = new ObjectInputStream(dateSocket.getInputStream());

} catch (UnknownHostException e) {

System.err.println("Server 'localhost' is unknown.");

System.exit(1);

} catch (IOException e) {

System.err.println("Error during connection to 'localhost'.");

System.exit(1);

}

...

1.- Import network, input/output and application dependent classes.

2.- Create a client socketconnected to the right server and port andget a stream from it.

Page 10: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 10 -

Sockets in Java: Client Side (II)

...

try {

Date serverDate = (Date) dateIn.readObject();

System.out.println ("Current server time: " + serverDate);

} catch (ClassNotFoundException e) {

System.err.println ("Not a Date object returned.");

System.exit(1);

}

dateIn.close();

dateSocket.close();

}

}

3.- Read and print theanswer from the server.

4.- Close streams and sockets used.

“Current server time: Thu Feb 05 14:45:00 CET 2004”

Page 11: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 11 -

Introduction to Datagrams in JavalA datagram is an independent message:

• Contains origin and destination addresses and ports.• Packet-based communication over UDP.

lHow to program with datagrams:• Server side

• Create a datagram socket in a “well-known” port.• Create an empty datagram packet.• Link the datagram to the socket and wait for client messages.• Get client data, address and port and send an answer.

• Client side• Create a datagram socket in any available port.• Create a datagram packet with destination address and port.• Send the datagram through the socket.• Create an empty packet as a placeholder for the server answer.• Link the packet to the socket and wait for the server response.• Get data from the datagram packet and process it.

Page 12: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 12 -

Datagram Programming

Server ClientKnownport Port

Datagramsocket

Datagramsocket

NetworkNetwork

Page 13: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 13 -

Datagrams in Java: Server Side (I)

import java.io.*;

import java.net.*;

import java.util.Date;

public class DateServer {

public static void main(String args[]) throws IOException {

DatagramSocket socket = null;

socket = new DatagramSocket(1313);

for (int i = 0; i < 10; i++) {

byte[] buf = new byte[256];

DatagramPacket packet = new DatagramPacket(buf, buf.length);

socket.receive(packet);

...

1.- Import network, input/output and application dependent classes.

2.- Create a datagram socket in a “well-known port.

3.- Empty packet.

4.- Wait for clients to fill the empty packet.

Page 14: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 14 -

Datagrams in Java: Server Side (II)

...

buf = new Date().toString().getBytes();

InetAddress address = packet.getAddress();

int port = packet.getPort();

packet = new DatagramPacket(buf, buf.length, address, port);

socket.send(packet);

} // end for loop.

socket.close();

}

}

5.- Prepare the response.

6.- Get client address and port.

7.- Send answer.

8.- Close the socket.

Page 15: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 15 -

Datagrams in Java: Client Side (I)import java.io.*;

import java.net.*;

import java.util.Date;

public class DateClient {

public static void main(String[] args) throws IOException, InterruptedException {

DatagramSocket socket = new DatagramSocket();

for (int i = 0; i < 10; i++) {

byte[] buf = new byte[256];

InetAddress address = InetAddress.getByName("localhost");

DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 1313);

socket.send(packet);

packet = new DatagramPacket(buf, buf.length);

socket.receive(packet);

...

1.- Import network, input/output and application dependent classes.

2.- Create a datagram socket in an available port

3.- Send anempty packetto the server.

4.- Wait for the server answer.

Page 16: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 16 -

Datagrams in Java: Client Side (II)...

String received = new String(packet.getData());

System.out.println("Current server time: " + received);

Thread.sleep(1500);

} //end for loop.

socket.close();

}

}

5.- Get server response and print it.

6.- Close the socket.

Page 17: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 17 -

Introduction to MulticastlMulticast is a datagram-based service to broadcast

messages to multiple clients.• Clients must subscribe to a multicast group.

lHow to program with Multicast:• Server side

• Create a datagram socket in any available port.• Create a datagram packet with the information to broadcast..• Send the packet to a class D (multicast) address and known port:

– Multicast addresses range: 224.0.0.0 - 239.255.255.255.• Client side

• Create a multicast socket with the service port.• Join the multicast group.• Create a packet for receiving messages from the group.• Leave the group.

lWarning:• Multicast messages are filtered by many routers.• Even if not filtered, they have a TTL (Time To Live).

Page 18: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 18 -

Multicast Programming

Server

NetworkNetwork

Dsocket

Client

Group:Address

+Port

Port

Msocket Port

ClientMsocket Port

ClientMsocket Port

joinsend

receive

Page 19: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 19 -

Multicast in Java: Server Sideimport java.io.*;

import java.net.*;

import java.util.Date;

public class MulticastDateServer {

public static void main(String args[]) throws IOException, InterruptedException {

DatagramSocket socket = new DatagramSocket(5050);

for (int i = 0; i < 10; i++) {

byte[] buf = new Date().toString().getBytes();

InetAddress group = InetAddress.getByName("230.0.0.1");

DatagramPacket packet;

packet = new DatagramPacket(buf, buf.length, group, 1313);

socket.send(packet);

Thread.sleep(1500);

}

socket.close();

}

}

1.- Import network, input/output and application dependent classes.

2.- Create socket in an available port.

3.- Prepare data to send.

4.- Send a packet to themulticast group addressand port.

5.- Close the socket.

Page 20: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 20 -

Multicast in Java: Client Sideimport java.io.*;

import java.net.*;

import java.util.Date;

public class MulticastDateClient {

public static void main(String[] args) throws IOException {

MulticastSocket socket = new MulticastSocket(1313);

InetAddress group = InetAddress.getByName("230.0.0.1");

socket.joinGroup(group);

for (int i = 0; i < 10; i++) {

byte[] buf = new byte[256];

DatagramPacket packet = new DatagramPacket(buf, buf.length);

socket.receive(packet);

String received = new String(packet.getData());

System.out.println("Current server time: " + received);

}

socket.leaveGroup(group);

socket.close();

}

}

1.- Import network, input/output and application dependent classes.

2.- Create a multicast socketand join the group.

3.- Empty packet toreceive group data.

4.- Print the answer.

5.- Leave the group and close the socket.

Page 21: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 21 -

Multiple Clients without Multicast (I)lMany applications require a server to maintain multiple

connections simultaneously.l The solution is multithreading:

import java.net.*;

import java.io.*;

import java.util.Date;

public class MultipleDateServer {

public static void main(String[] args) throws IOException {

ServerSocket serverSocket = new ServerSocket(1313);

ClientAttender attender;

for (int i = 0; i < 10; i++) {

attender = new ClientAttender(serverSocket.accept());

attender.start();

}

serverSocket.close();

}

}

...

1.- Import network, input/output and application dependent classes.

2.- Create a server socketin a known port.

3.- Create a new thread andstart it on acceptance.

4.- The server socket is closed after attending 10 clients.

Page 22: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 22 -

Multiple Clients without Multicast (II)...

class ClientAttender extends Thread {

Socket clientSocket;

ObjectOutputStream dateOut;

public ClientAttender(Socket cs) {

clientSocket = cs;

}

public void run() {

try {

dateOut = new ObjectOutputStream

(clientSocket.getOutputStream());

dateOut.writeObject(new Date());

dateOut.close();

clientSocket.close();

} catch (IOException ioe) {

System.err.println("Error writing to the socket");

}

}

}

5.- A reference to the socket created in accept() is kept.

6.- Get an output stream from the socket,send the object and thenclose the stream and the socket

Page 23: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 23 -

Multiple clients programming

Server

Serversocket

Client

socket

Clientsocket

Clientsocket

socket

socket

socket

MainThread

newThread

newThread

newThread

Page 24: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 24 -

Java NIOlNew classes for input/output (since JDK 1.4).

lAllow:• File locking: useful when sharing files.• Regular Expressions: pattern recognition.• Buffer Views: ways to interprete byte buffers.• Byte Swabbing: big-little endianness.• Direct Buffers: access to native memmory.• Memmory-mapped files: map and share files in RAM.• Scattering Reads and Gathering Writes: compose buffers.• Direct Channel Transfers: data transfers (DMA).• Non-blocking sockets:

• Contrary to traditional java.io.• Multiplexed I/O:

• Multiple clients without need of multithreading.

Page 25: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 25 -

Concepts in Java NIOlBuffers

• Containers of data.

lChannels• Data carriers:

• File channels.• Pipe channels.• Socket channels.

lSelector• Multiplexer of registered channels.• Select the channels which needs processing.

lKey• Relationship between a selector and a channel.

• Operations of interest.• Ready operations.

Page 26: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 26 -

Concepts in Java NIO (UML)

SelectorSelector

SelectionKeySelectionKey

OperationOperation

ChannelChannel

readyinterest

* *

1 1

{subset}

**is registered

Page 27: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 27 -

Programming with Java NIOlCreate a Channel.

lRegister it to a Selector.• Mark the operations of interest to detect in the channel.

lCall to the select operation of Selector.• Returns a number of the keys whose ready set has changed.

lGet the selected keys:• Check ready operations for each key.• Do appropriate processing.• Remove the key from the selected keys set.

• That clears the ready operations set of the key.

lRecall select operation.

Page 28: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 28 -

Java NIO: Server Side (I)import java.io.*;

import java.net.*;

import java.nio.*;

import java.nio.channels.*;

import java.util.*;

public class DateServerNIO {

static final int port = 1313;

void process() throws IOException {

ServerSocketChannel serverChannel = ServerSocketChannel.open();

serverChannel.socket().bind(new InetSocketAddress(port));

System.out.println("Socket bound");

Selector mux = Selector.open();

serverChannel.configureBlocking(false);

serverChannel.register(mux, SelectionKey.OP_ACCEPT);

System.out.println("Channel registered");

...

1.- Import network, input/output and application dependent classes.

2.- Create a server socket channeland bind it to a known port.

3.- Register the channel in a selector.

Page 29: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 29 -

Java NIO: Server Side (II)while (mux.select() > 0) {

Iterator keyIt = mux.selectedKeys().iterator();

while (keyIt.hasNext()) {

SelectionKey key = (SelectionKey) keyIt.next();

if (key.isAcceptable()) {

ServerSocketChannel server =

(ServerSocketChannel) key.channel();

SocketChannel channel = server.accept();

if (channel != null) {

// channel.configureBlocking(false);

// channel.register (mux, SelectionKey.OP_READ);

ObjectOutputStream dateOut =

new ObjectOutputStream

(Channels.newOutputStream (channel));

dateOut.writeObject(new Date());

}

}

keyIt.remove();

}

}

4.- Iterate over the selected keys.

5.-The server socket is ready for accept.

6.- Send the date.

6’.- Register the new channel.

Page 30: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 30 -

Java NIO: Server Side (III)

...

} //end of process

public static void main(String args[]) {

try{

new DateServerNIO().process();

} catch (IOException ioe) {

ioe.printStackTrace ();

}

}

}

1.- Start the server.

Page 31: networking in java

RMI

Communicating with Remote Objects

in Java

Page 32: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 32 -

RMI (Remote Method Invocation)lRMI purpose is to make calls to remote objects as if

they were local objects (higher level abstraction compared to sockets).

l The idea is not new:• RPC (Remote Procedure Call).• CORBA (Common Object Request Broker Architecture).• DCOM (Distributed Component Object Model).• Web Services.

lRMI is limited to Java programs.• New extensions for interoperability with CORBA (RMI-IIOP).

lRMI allows dynamic code loading.• Automatically extends the behavior of an application.

Page 33: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 33 -

Introduction to RMIlDesign and implement the distributed components:

• Define the remote interfaces.• Implement the remote objects.• Implement the clients.

lCompile sources and create stubs:• Compile sources with “javac”.• Generate stubs with “rmic”.

lMake classes accessible from the network:• Code to be downloaded must be available.• Use a web server if dynamic code downloading is needed.

lStart the application:• Start the RMI registry.• Start the server.• Start the clients.

Page 34: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 34 -

Programming with RMI

RemoteObject

RemoteObject

Web ServerWeb Server

Remote Object Stub

Remote Object Stub

RMI registryRMI registry

ServerClient

Network

Client applicationClient application

lookup

Remote Object Stub

Remote Object Stub

download

Local call

rmic

RemoteInterface

rebind

Remote call

Page 35: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 35 -

Designing the remote InterfacelExtends the interface java.rmi.Remote.l Its procedures can fail due to connection problems:

• They are declared to throw java.rmi.RemoteException.

import java.rmi.Remote;

import java.rmi.RemoteException;

public interface DateService extends Remote {

Date getCurrentDate() throws RemoteException;

}

Page 36: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 36 -

Implementing the Remote Objectl The Remote Object:

• Extends java.rmi.server.UnicastRemoteObject.• Implements the defined remote interface.

lAt server startup:• Set a Security Manager.• Set a name for the service, typically:

• “//<hostname>/<service>” or• “rmi://<hostname>/<service>”

• Bind the name to the RMI registry.• Exit main routine.

l The Garbage collector does not reclaim the object:• The RMI registry process keeps a reference to the object.

Page 37: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 37 -

Date Server Implementation (I)import java.rmi.*;

import java.rmi.server.*;

import java.util.Date;

public class DateServer extends UnicastRemoteObject implements DateService {

public DateServer() throws RemoteException {

super();

}

public Date getCurrentDate() throws RemoteException {

return (new Date());

}

public static void main(String[] args) {

if (System.getSecurityManager() == null) {

System.setSecurityManager(new RMISecurityManager());

}

String registryName = "rmi://localhost/Date";

...

1.- Import RMI and application dependent classes.

2.- The constructor of the remote object also throwsRemoteException

3.- Implement the methods of the interface.

4.- Install a security manager

Page 38: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 38 -

Date Server Implementation (II)try {

DateService dateProvider = new DateServer();

Naming.rebind(registryName, dateProvider);

System.out.println("DateServer bound");

} catch (Exception e) {

System.err.println("DateServer exception: " + e.getMessage());

e.printStackTrace();

}

}

}

5.- Create the object and bind itto a name in the registry.

Page 39: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 39 -

Implementing the ClientlAt startup, the Client:

• Sets up a Security Manager.• Gets a reference of the remote object.

l To get the reference:• The client performs a lookup using the registered name.

lDuring operation• The client calls the methods of the remote object as usual.• Note that the client only needs to know the remote interface.

lNotes:• Remember that remote methods can fail due to problems with

the network connection.• Minimize network traffic for increasing performance.

Page 40: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 40 -

Date Client Implementation (I)import java.rmi.*;

import java.util.Date;

public class DateClient {

public static void main(String args[]) {

if (System.getSecurityManager() == null) {

System.setSecurityManager(new RMISecurityManager());

}

try {

String name = "rmi://localhost/Date";

DateService daytimeServer;

daytimeServer = (DateService) Naming.lookup(name);

System.out.println(daytimeServer.getCurrentDate().toString());

} catch (Exception e) {

System.err.println("DateClient exception: " + e.getMessage());

e.printStackTrace();

}

}

}

2.- Install a security manager

1.- Import RMI and application dependent classes.

3.- Get a reference to the remote object.

4.- Call a remote method.

Page 41: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 41 -

RMIClAfter compiling the classes, you have to generate the

stub for the server class.

l The stub acts as a proxy class for the client.

lUse “rmic –v1.2 <serverclass>” for programs compiled with JDK 1.2 or above (recommended).

l The stub generated must be accessible to the client:• Have a local copy in the client machine.• Put it under a web server in the server machine.

l Important! When using a web server:• Make sure that the stub is NOT in the CLASSPATH when

running “rmiregistry”.

Page 42: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 42 -

Java Policyl The security manager will not let open any port unless

the ones authorized by the policy.

l The policy is a file which describes the right accesses of external processes to the local machine.

l The policy above grants access to all ports above 1023 and “connect” access to port 80 (HTTP). This is useful for downloading code when using a web server.

grant {

permission java.net.SocketPermission "*:1024-65535",

"connect,accept";

permission java.net.SocketPermission "*:80", "connect";

};

Page 43: networking in java

2/6/2004© Rodrigo García García, EPFL - slide 43 -

Starting the ApplicationlStarting the RMI registry:

• “start rmiregistry [port]” (Windows).• “rmiregistry [port] &” (UNIX).

lStarting the server:• “java -Djava.security.policy=java.policy DateServer”.

lStarting the client:• “java -Djava.security.policy=java.policy DateClient”.

lWhen using a web server, specify also:• Codebase:

• “-Djava.rmi.server.codebase=http://host:port/webfolder/”

• Hostname:• “-Djava.rmi.server.hostname=lglpc31.epfl.ch”

• There is a simple HTTP server in Sun’s RMI tutorial.• ClassFileServer.