Networking in Java
Networking in Java
5.2.2004
Introduction to Networking
Facilities Application
Application HTTP,
HTTP,FTP…
FTP…
Data transformation Presentation
Presentation SSL, XDR…
Synchro, recovery Session
Session RPC, RMI…
Reliability, multiplex Transport
Transport TCP,
TCP,UDPUDP
Routing Network
Network IP,
IP,ICMP
ICMP
Error/flow control Link
Link HDLC,
HDLC,LLC…
LLC…
Connectors, volts, wires Physical
Physical RJ45,
RJ45,RS232…
RS232…
Duplex communication
Socket
Known Socket Port Client
Server port Server
accept
Socket
connect
dateOut.close();
clientSocket.close(); 5.- Close streams and sockets used.
serverSocket.close();
}
}
- Note: Full objects can be sent through the streams, but they must be Serializable.
try {
Date serverDate = (Date) dateIn.readObject(); 3.- Read and print the
System.out.println ("Current server time: " + serverDate); answer from the server.
} catch (ClassNotFoundException e) {
System.err.println ("Not a Date object returned.");
System.exit(1);
}
dateIn.close();
4.- Close streams and sockets used.
dateSocket.close();
}
}
import java.io.*;
import java.net.*; 1.- Import network, input/output and application dependent classes.
import java.util.Date;
...
DatagramSocket socket = new DatagramSocket(); 2.- Create a datagram socket in an available port
packet = new DatagramPacket(buf, buf.length); 4.- Wait for the server answer.
socket.receive(packet);
...
String received = new String(packet.getData()); 5.- Get server response and print it.
System.out.println("Current server time: " + received);
Thread.sleep(1500);
} //end for loop.
l Warning:
• Multicast messages are filtered by many routers.
• Even if not filtered, they have a TTL (Time To Live).
Network
Network
Group: join
send Address
Server Port Dsocket
+
Port Msocket Port Client
receive
Thread.sleep(1500);
}
socket.leaveGroup(group);
5.- Leave the group and close the socket.
socket.close();
}
}
socket Client
Main
Serversocket
Thread
new
socket
Server Thread
socket Client
new
socket
Thread
new
socket
Thread
socket Client
l Allow:
• 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.
l Channels
• Data carriers:
• File channels.
• Pipe channels.
• Socket channels.
l Selector
• Multiplexer of registered channels.
• Select the channels which needs processing.
l Key
• Relationship between a selector and a channel.
• Operations of interest.
• Ready operations.
SelectionKey
SelectionKey
1 1
{subset}
Operation
Operation
interest ready
* *
l Register it to a Selector.
• Mark the operations of interest to detect in the channel.
...
while (keyIt.hasNext()) {
SelectionKey key = (SelectionKey) keyIt.next();
if (key.isAcceptable()) {
ServerSocketChannel server =
(ServerSocketChannel) key.channel();
5.-The server socket is ready for accept.
SocketChannel channel = server.accept();
if (channel != null) {
// channel.configureBlocking(false);
6’.- Register the new channel.
// channel.register (mux, SelectionKey.OP_READ);
ObjectOutputStream dateOut =
new ObjectOutputStream
6.- Send the date.
(Channels.newOutputStream (channel));
dateOut.writeObject(new Date());
}
}
keyIt.remove();
}
}
} //end of process
Communicating with
Remote Objects
in Java
RMI (Remote Method Invocation)
l RMI purpose is to make calls to remote objects as if
they were local objects (higher level abstraction
compared to sockets).
lookup RMI
RMIregistry
registry
Client
Clientapplication
application rebind
Remote
Interface
Local call Remote
Remote
Remote call Object
Object
Remote
RemoteObject
Object
Stub
Stub
rmic
Web
WebServer
Server
download
Remote
RemoteObject
Object
Stub
Stub
Network
import java.rmi.Remote;
import java.rmi.RemoteException;
l At 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 During operation
• The client calls the methods of the remote object as usual.
• Note that the client only needs to know the remote interface.
l Notes:
• Remember that remote methods can fail due to problems with
the network connection.
• Minimize network traffic for increasing performance.
try {
String name = "rmi://localhost/Date";