Professional Documents
Culture Documents
Advanced Java Programming: Unit One: Networking
Advanced Java Programming: Unit One: Networking
Lee
John
Morris
Overview
Recommended Reading
Java Network Programming, Elliotte Rusty
Harold, OReilly and Associates, 1997,
ISBN 1-56592-227-1
TCP/IP Network Administration, Second
Edition, Craig Hunt, OReilly and
Associates, 1997, ISBN 1-56592-322-7
The Java Developers connection:
http://www.javasoft.com/jdc
The Javadoc documentation
Network Programming
Mechanisms by which software running on
two or more computational devices can
exchange messages
Desktop Computers
PDAs / Set Top Boxes / Mobile Telephones?
Applications
Application Protocols
Unicast Broadcast Multicast
Streams
Datagrams
Addressing & Routing
Physical Transport
java.net.InetAddress (1)
Abstraction of a network address
Currently uses IPv4 (a 32 bit address)
Will support other address formats in
future
Allows an address to be obtained from a
host name and vice versa
Is immutable (is a read-only object)
Create an InetAddress object with the address
you need and throw it away when you have
finished
java.net.InetAddress (2)
Static construction using a factory method
InetAddress getByName(String hostName)
hostName can be host.domain.com.au, or
hostName can be 130.95.72.134
InetAddress getLocalHost()
java.net.ServerSocket (1)
Listens on well-known port for incoming
connections
Creates a dynamically allocated port for
each newly established connection
Provides a Socket connected to the new
port
Maintains a queue to ensure that
prospective clients are not lost
java.net.ServerSocket (2)
Construction:
ServerSocket(int port, int backlog)
Allows up to backlog requests to queue waiting for the
server to deal with them
java.net.Socket (1)
Provides access to TCP/IP streams
Bi-directional communication between
sender and receiver
Can be used to connect to a remote
address and port by using the constructor:
Socket(String remoteHost, int port)
java.net.Socket (2)
Can obtain access to input and output
streams
Input stream allows reception of data from
the other party
InputSteam getInputStream()
Client (sid)
s = new Socket
(fred, 80)
Server (fred)
2037
80
2037
1583
ServerSocket ss.
s = ss.accept()
Socket s
s.getInputStream()2037
s.getOuputStream()
1583 s.getInputStream()
s.getOuputStream(
(UnknownHostException uhe) {
Very unlikely to occur
(IOException ioe) {
May occur if the client misbehaves?
java.net.DatagramPacket (1)
DatagramPackets normally used as short
lived envelopes for datagram messages:
Used to assemble messages before they are
dispatched onto the network,
or dismantle messages after they have been
received
Destination/source address
Destination/source port number
Data bytes constituting the message
Length of message data bytes
java.net.DatagramPacket (2)
Construction:
DatagramPacket(byte[] data, int length)
java.net.DatagramSocket (1)
Used to represent a socket associated with
a specific port on the local host
Used to send or receive datagrams
Note: there is no counterpart to
java.net.ServerSocket! Just use a
DatagramSocket with a agreed port
number so others know which address and
port to send their datagrams to
java.net.DatagramSocket (2)
Construction:
DatagramSocket(int port)
Uses a specified port (used for receiving datagrams)
DatagramSocket()
Allocate any available port number (for sending)
sea.datagram.DatagramSender
This example sends datagrams to a
specific host (anywhere on the Internet)
The steps are as follows:
Create a new DatagramPacket
Put some data which constitutes your message
in the new DatagramPacket
Set a destination address and port so that the
network knows where to deliver the datagram
Create a socket with a dynamically allocated
port number (if you are just sending from it)
Send the packet through the socket onto the
network
sea.datagram.DatagramSender
byte[] data = This is the message.getBytes();
DatagramPacket packet =
new DatagramPacket(data, data.length);
// Create an address
InetAddress destAddress =
InetAddress.getByName(fred.domain.com);
packet.setAddress(destAddress);
packet.setPort(9876);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
sea.datagram.DatagramReceiver
The steps are the reserve of sending:
Create an empty DatagramPacket (and allocate
a buffer for the incoming data)
Create a DatagramSocket on an agreed socket
number to provide access to arrivals
Use the socket to receive the datagram (the
thread will block until a new datagram arrrives)
Extract the data bytes which make up the
message
sea.datagram.DatagramReceiver
// Create an empty packet with some buffer space
byte[] data = new byte[1500];
DatagramPacket packet =
new DatagramPacket(data, data.length);
DatagramSocket socket = new DatagramSocket(9876);
// This call will block until a datagram arrives
socket.receive(packet);
// Convert the bytes back into a String and print
String message =
new String(packet.getData(), 0, packet.getLength());
System.out.println("message is " + message);
System.out.println("from " + packet.getAddress());
Broadcasting
Broadcasting allows a single datagram to
be sent to a group of listeners
The group consists of all the computers
within the local network
The previous code examples can be used
for broadcasting
Just change the address: each network
has a unique broadcast address
IP addresses revisited
Each 32 bit IP number consists of two
components:
The network address
The unique international address of the
network
The host address
The unique address of a specific host in the
net
0...
Class B
10...
Class C
110...
85
35
87
Broadcast addresses
CIIPS has a class C network which has
the address 130.95.72
This portable computer has host address
134 within the CIIPS network
Each network has a single host address
which is set aside for broadcasts (either all
one bits or all zero bits)
The CIIPS network uses broadcast address
130.95.72.255
Broadcasts are never routed onto other
networks
Multicasting (1)
Described in RFC1112 (August 1989)
Multicasting allows distribution of a
datagram to a group of listeners who are
not within the local network
Routers between networks need to pass
multicast datagrams. . but many do not!
The MBONE is a way of tunneling
datagrams across the Internet between
islands of multicast activity
Multicasting (2)
Multicasts are also sent to a special
address (known as a group)
Multicast groups need to be agreed in
advance. They are not derived from a
specific network/host address
Multicast groups identify a subject area (or
stream of content) rather than a specific
computer or network. They are more like a TV
channel number than a telephone number.
Multicasting (3)
To send to (or receive from) a multicast
group it is first necessary to register
interest in the group
This results in an Internet Group Management
Protocol (IGMP) message being sent to your
router (RFCs 988/1112/2236)
224.0.0.1
All hosts within local subnet
224.0.1.7
Audio news multicast
224.0.1.12
Video from IETF meetings
224.0.1.20
Expts. within local subnet
224.0.1.25
NBC Professional News
There are 268 million multicast addresses
(in IPv4) with 65 thousand ports in each!
java.net.MulticastSocket
Subclass of java.net.DatagramSocket
Constructed the same way
Adds some extra methods:
void joinGroup(InetAddress mcastGroup)
Enter the specifies group so that you can send or receive
datagrams
void leaveGroup(InetAddress mcastGroup)
Leave a group that you previously joined
void setTimeToLive(int ttl)
Sets how far your datagrams will travel before routers
ignore them
int getTimeToLive()
sea.datagram.MulticastSender
Sending similar to the previous example. .
. . .but must register with the multicast
group and decide the longevity
The steps involved are:
sea.datagram.MulticastSender
InetAddress multicastGroup =
InetAddress.getByName(multicastGroupAddr);
MulticastSocket socket = new MulticastSocket();
socket.joinGroup(multicastGroup);
socket.setTimeToLive(5);
byte[] data = This is the message.getBytes();
DatagramPacket datagram =
new DatagramPacket(data, data.length);
datagram.setAddress(multicastGroup);
datagram.setPort(9876);
socket.send(datagram);
socket.leaveGroup(multicastGroup);
sea.datagram.MulticastReceiver
The steps are:
sea.datagram.MulticastReceiver
InetAddress multicastGroup =
InetAddress.getByName(multicastGroupAddr);
MulticastSocket socket = new MulticastSocket(9876);
socket.joinGroup(multicastGroup);
byte[] data = new byte[1000];
DatagramPacket packet =
new DatagramPacket(data, data.length);
socket.receive(packet);
String message = new String(
packet.getData(), 0, packet.getLength());
socket.leaveGroup(multicastGroup);
Homework
Read through the code samples to
convince yourself you understand whats
going on
Sample code can be downloaded from
http://ciips.ee.uwa.edu.au/~gareth
If you can, run the examples
Comments, Suggestions. . .
How was the presentation paced?
Was there enough (or too much) technical
content?
Any areas of particular interest?
Comments regarding presentation style?