Académique Documents
Professionnel Documents
Culture Documents
Overview
Javas network support Addressing other machines Communicating using TCP/IP Communicating using UDP Broadcasting and multicasting
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?
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
Dealt with in this Unit One. Dealt with in this Unit Two.
Works with Datagrams: small discrete packets of data (rather like a letter)
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()
public static void main(String[] args) { try { InetAddress inet1 = InetAddress.getByName("asp.ee.uwa.edu.au"); System.out.println( "HostAddress=" + inet1.getHostAddress()); InetAddress inet2 = InetAddress.getByName("130.95.72.134"); System.out.println("HostName=" + inet2.getHostName()); if (inet1.equals(inet2)) System.out.println("Addresses are equal"); } catch (UnknownHostException uhe) { uhe.printStackTrace(); } }
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
ServerSocket ss. s = ss.accept()
2037
1583
Socket s
1583 s.getInputStream()
s.getOuputStream()
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
java.net.DatagramPacket (2)
Construction:
DatagramPacket(byte[] data, int length)
DatagramPackets are not immutable so, in principle you can reuse then, but . . Experience has shown that they often misbehave when you do -- create a new one, use it once, throw it away!
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);
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
Class B
Class C
10...
110... Network Address Byte Host Address Byte
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.
The IETF has set aside addresses from 224.0.0.1 to 239.255.255.255 specifically for multicasting
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)
Then a datagram is created, addressed to the group (and the chosen port) Java has a specialised socket for multicasting: java.net.MulticastSocket
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:
Create the MulticastSocket. Join the multicast group(s) (on startup). Create the DatagramPacket. Send the packet through the socket. Leave the multicast group (on exit).
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:
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); 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?