AJava Networking
AJava Networking
AJava Networking
Lee John
Morris
Gareth Lee
Department of Electrical and Electronic Engineering,
University of Western Australia
Overview
• Java’s network support
• Addressing other machines
• Communicating using TCP/IP
• Communicating using UDP
• Broadcasting and multicasting
Recommended Reading
• Java Network Programming, Elliotte Rusty
Harold, O’Reilly and Associates, 1997,
ISBN 1-56592-227-1
• TCP/IP Network Administration, Second
Edition, Craig Hunt, O’Reilly and
Associates, 1997, ISBN 1-56592-322-7
• The Java Developer’s 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?
• Java is a network centric programming
language
• Java abstracts details of network
implementation behind a standard API
• Portable (and future proof) . . .
• but may be rather limiting
A programming model for network
communications
2037 1583
Socket s
while (isStillServing()) {
Socket session = agreedPort.accept();
respond(session);
session.close();
}
agreedPort.close();
}
catch (UnknownHostException uhe) {
// Very unlikely to occur
}
catch (IOException ioe) {
// May occur if the client misbehaves?
}
}
A sample TCP client
public static void main(String[] args)
{
try {
InetAddress server = InetAddress.getByName(args[0]);
Socket connection =
new Socket(server, AGREED_PORT_NUMBER);
makeRequestToServer(connection);
getReplyFromServer(connection);
connection.close();
}
catch (UnknownHostException uhe) {
// arg[0] is not a valid server name or IP address
}
catch (IOException ioe) {
// The connection to the server failed somehow:
// the server might have crashed mid sentence?
}
}
What are datagrams?
• Datagrams are discrete packets of data
• Each is like a parcel that can be addressed
and sent to an recipient anywhere on the
Internet
• This is abstracted as the User Datagram
Protocol (UDP) in RFC768 (August 1980)
• Most networks cannot guarantee reliable
delivery of datagrams
Why use datagrams?
• Good for sending data that can naturally
be divided into small chunks
• Poor for (lossless) stream based
communications
• Makes economical use of network
bandwidth (up to 3 times the efficiency of
TCP/IP for small messages)
• Datagrams can be locally broadcast or
multicast (one-to-many communication)
Application using datagrams
• UDP can be used for economical point-to-
point communications over LANs
• Unix NFS (Network File System)
• NIS (a.k.a. Yellow Pages)
• Datagrams can be used for one-to-many
communication:
• Local network broadcasting;
• Multicasting (MBONE)
• but there is no way to create one-to-many
streams using TCP/IP
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
• Has the following attributes:
• 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)
// Create an address
InetAddress destAddress =
InetAddress.getByName(“fred.domain.com”);
packet.setAddress(destAddress);
packet.setPort(9876);
192 . 85 . 35 . 87
Class A 0...
Class B 10...
Class C 110...
socket.send(datagram);
socket.leaveGroup(multicastGroup);
sea.datagram.MulticastReceiver
• The steps are:
• Create a multicast socket on an agreed port.
• Join the multicast group (on startup).
• Create an empty datagram.
• Wait for datagram to be delivered on the
socket.
• Unpack and use the datagram.
• Leave the multicast group (on exit).
sea.datagram.MulticastReceiver
InetAddress multicastGroup =
InetAddress.getByName(multicastGroupAddr);
MulticastSocket socket = new MulticastSocket(9876);
socket.joinGroup(multicastGroup);
socket.leaveGroup(multicastGroup);
Useful sources of information
• The Internet Engineering Task Force (IETF)
which is at http://www.ietf.org -- you will be
able to download RFCs from here
• Multicasting try reading the HOWTO which
is available from the URL:
http://ftp.southcom.com.au/LDP/HOWTO/...
Multicast-HOWTO.html
Homework
• Read through the code samples to
convince yourself you understand what’s
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?