|
Distributed Computing This website demonstrates using wikis as teaching and learning tool. The course instructor is also happy to share the teaching materials here with those who find it readable. |
Lecture /
Java Network Programming - Part 2A Distributed Computing Lecture by Steven Choy Lecture Overview: Datagram networking - Connection-oriented vs connectionless networking - Classes DatagramPacket and DatagramSocket - Datagram applications - Multicast networking Concepts - Class MulticastSocket - Multicast applications Datagram and Multicast NetworkingConnection-oriented networking
Connectionless networking
Uses of datagram networkingWhy use UDP rather than TCP?
Programming UDP (vs TCP)
Class DatagramPacket
// receive
DatagramPacket(byte buffer[], int length)
// transmit & receive
DatagramPacket(byte buffer[], int length, InetAddress address, int port)
InetAddress getAddress()
int getPort()
byte[] getData()
int getLength()
void setAddress(InetAddress address)
void setPort(int port)
void setData(byte[] buffer)
void setLength(int length)
Class DatagramSocket
// transmit
DatagramSocket() throws SocketException;
// receive & transmit
DatagramSocket(int port) throws SocketException;
DatagramSocket(int port, InetAddress local) throws SocketException;
void send(DatagramPacket packet) throws IOException
void receive(DatagramPacket packet) throws IOException
InetAddress getLocalAddress()
int getLocalPort()
void close()
setSoTimeout, getSoTimeout, setSendBufferSize, getSendBufferSize, setReceiveBufferSize, getReceiveBufferSize, connect, disconnect, getInetAddress, getPort
Sending and Receiving UDP Packet
Program to receive a packet
DatagramSocket socket = new DatagramSocket (port); byte buffer[] = new byte[65508]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); socket.receive (packet); InetAddress fromAddress = packet.getAddress (); int fromPort = packet.getPort (); int length = packet.getLength (); byte[] data = packet.getData (); socket.close(); Program to send a packet
DatagramSocket socket = new DatagramSocket (); DatagramPacket packet = new DatagramPacket (data, data.length, InetAddress.getByName("www.ouhk.edu.hk"), 1782); socket.send (packet); socket.close(); A UDP echo serverimport java.net.*; import java.io.*; public class UDPEchoServer { protected int port; public UDPEchoServer (int port) { this.port = port; } public static void main (String[] args) throws IOException { if (args.length != 1) throw new IllegalArgumentException("Syntax: UDPEchoServer <port>"); UDPEchoServer server = new UDPEchoServer (Integer.parseInt (args[0])); server.execute (); } public void execute () throws IOException { DatagramSocket socket = new DatagramSocket (port); while (true) { System.out.println("Waiting to receive packets"); byte buffer[] = new byte[65508]; DatagramPacket packet = new DatagramPacket (buffer, buffer.length); socket.receive (packet); System.out.print("* A packet is received and is echoed back to: "); System.out.println(packet.getAddress()+":"+packet.getPort()); DatagramPacket response = new DatagramPacket( packet.getData(), packet.getLength(), packet.getAddress(), packet.getPort() ); socket.send (response); } } } A UDP echo clientimport java.net.*; import java.io.*; public class UDPEchoClient { protected DatagramSocket socket; protected DatagramPacket packet; public void execute (String host, int port, String message) throws IOException { socket = new DatagramSocket (); buildPacket (message, host, port); socket.send (packet); receivePacket (); socket.close (); } protected void buildPacket (String message, String host, int port) throws IOException { ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream (); DataOutputStream dataOut = new DataOutputStream (byteArrayOut); dataOut.writeUTF (message); byte[] data = byteArrayOut.toByteArray (); packet = new DatagramPacket (data, data.length,InetAddress.getByName (host), port); } protected void receivePacket () throws IOException { byte buffer[] = new byte[65508]; DatagramPacket packet = new DatagramPacket (buffer, buffer.length); socket.receive (packet); ByteArrayInputStream byteArrayIn = new ByteArrayInputStream(packet.getData(), 0, packet.getLength()); DataInputStream dataIn = new DataInputStream (byteArrayIn); String result = dataIn.readUTF (); System.out.println ("Received: " + result + "."); } public static void main (String[] args) throws IOException { if (args.length != 3) throw new IllegalArgumentException ("Syntax: UDPEchoClient <host> <port> <message>"); UDPEchoClient client = new UDPEchoClient(); client.execute(args[0], Integer.parseInt (args[1]), args[2]); } } A daytime server & client
DNS over UDP
Multicast networking
Multicast group addressAn analogue
Multicast networking
MBone
Importance & limitations
Class MulticastSocket
// transmit MulticastSocket() // receive & transmit MulticastSocket(int port)
void joinGroup(InetAddress group) throws IOException void leaveGroup(InetAddress group) throws IOException void send(DatagramPacket packet, byte ttl) throws IOException void setTimeToLive(int ttl) throws IOException int getTimeToLive() throws IOException setTTL, getTTL, setInterface, getInterface Programming multicast
Testing multicast programs
A daytime broadcast serverimport java.net.*; import java.io.*; import java.util.*; public class DaytimeBroadcastServer { public static final int defaultPort = 1234; public static void main (String args[]) throws Exception { if (args.length > 1 ) throw new IllegalArgumentException ("Syntax: DaytimeBroadcastServer [<multicastgroup>]"); InetAddress multicastGroup = InetAddress.getByName( args.length==0 ? "234.5.6.7": args[0]); MulticastSocket socket = new MulticastSocket(); // socket.joinGroup(multicastGroup); while (true) { Thread.sleep(5000); System.out.println("Broadcast Data ..."); String str = (new Date()).toString(); byte[] data = str.getBytes(); DatagramPacket packet = new DatagramPacket( data, data.length, multicastGroup, defaultPort); socket.send(packet); } } } A daytime broadcast clientimport java.net.*; import java.io.*; import java.util.*; public class DaytimeBroadcastClient { public static void main (String args[]) throws Exception { if (args.length > 1 ) throw new IllegalArgumentException ("Syntax: DaytimeBroadcastServer [<multicastgroup>]"); InetAddress multicastGroup = InetAddress.getByName( args.length==0 ? "234.5.6.7": args[0]); MulticastSocket socket = new MulticastSocket( DaytimeBroadcastServer.defaultPort); socket.joinGroup(multicastGroup); byte[] buffer = new byte[80]; DatagramPacket packet = new DatagramPacket (buffer, buffer.length); socket.receive(packet); String str = new String(packet.getData()); System.out.println("Time received from " + packet.getAddress() + " is: " + str); } } Things to be done with multicast
Extra materials for studying Java UDP programmingFrom the Java Tutorials > Custom Networking > All About Datagrams.
From School of Computing, Napier University, Edinburgh
Let's start with a very simple example using UDP to transfer a single line of text. We will eventually extend this to become a very primitve text-based "chat" program.
Thanks for ReadingIf you would rather like to have this lecture note in printed format, please click the print action link in the top right corner. You are welcome to make contribution to this lecture note. You can suggest links to useful resources or draw illustration to aid understanding of some topics. If you find any problem in this lecture note, please feel free to tell Steven via the following email address. ![]() |