CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina
2/19/20042 ICMP Source Quench Error type(4)code(0) checksum IP header + first 8 bytes of original datagram data unused (must be 0)
2/19/20043 Address Bindings of UDP Server Three types of address bindings localIP.lport and foreignIP.fport localIP.lport and *.* *.lport and *.*
2/19/20044 Broadcasting and Multicasting Send message from single source to all or multiple destinations Only apply to UDP (why?) Broadcasting is to send a message to every host on the cable Multicasting is to send a message to a set of hosts that belong to a multicast group
2/19/20045 Broadcasting Four types of IP broadcast addresses Limited broadcast: Net-directed broadcast: e.g. netid for a class A network ID Subnet-directed broadcast: all “1” bits for host ID but need a specific subnet ID All-subnets-directed broadcast: need to know subnet mask
2/19/20046 Multicasting Two types of services Delivery to multiple destinations Solicitation of servers by clients
2/19/20047 Multicast Group Address Combination of high-order 4 bits of “1110” and multicast group ID Range is –
2/19/20048 Host Group Set of hosts listening to a particular IP multicast address Can span multiple networks Membership is dynamic; hosts can join and leave at any time Permanent host groups : all systems on this subnet : all routers on this subnet : Network Time Protocol (NTP) : RIP-2
2/19/20049 Class MulticastSocket Constructors MulticastSocket() MulticastSocket(int port)
2/19/ Class MulticastSocket Methods void joinGroup(InetAddress group) throws IOException void leaveGroup(InetAddress group) throws IOException void setTimeToLive(int ttl) throws IOException void setTTL(byte ttl) throws IOException int getTimeToLive() throws IOException byte getTTL() throws IOException void send(DatagramPacket packet, byte ttl) throws IOException void setInterface(InetAddress address) throws SocketException InetAddress setInterface() throws SocketException
2/19/ Class MulticastSocket Exceptions IOException SecurityException
2/19/ Sending Multicast Packets // byte[] data // InetAddress multicastGroup // int multicastPort MulticastSocket socket = new MulticastSocket(); DatagramPacket packet = new DatagramPacket (data, data.length, multicastGroup, multicastPort); socket.send(packet, (byte) 64); socket.close();
2/19/ Receiving Multicast Packets MulticastSocket socket = new MulticastSocket(multicastPort); Socket.joinGroup(multicastGroup); byte buffer[] = new byte[65508]; DatagramPacket packet = new DatagramPacket(); socket.receive(packet); InetAddress fromAddress = packet.getAddress(); int fromPort = packet.getPort(); int length = packet.getLength(); byte[] data = packet.getData(); // … socket.leaveGroup(multicastGroup); socket.close();
2/19/ A Peer-to-Peer Multicast Chat System Each client multicasts its message to other clients No server is involved; all clients communicate as peers Open a chat frame and start a thread that listens for incoming packets
2/19/ MulticastChat.java /* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN X * * * * Copyright (c) Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */ import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; public class MulticastChat implements Runnable, WindowListener, ActionListener { // public MulticastChat (InetAddress group, int port) … // public synchronized void start () throws IOException … // public void windowOpened (WindowEvent event) … // public void windowClosing (WindowEvent event) … // public void actionPerformed (ActionEvent event) … // public void run () … // public static void main (String[] args) throws IOException … }
2/19/ Constructor MulticastChat protected InetAddress group; protected int port; public MulticastChat (InetAddress group, int port) { this.group = group; this.port = port; initAWT (); } // protected void initAWT () …
2/19/ Method initAWT protected Frame frame; protected TextArea output; protected TextField input; protected void initAWT () { frame = new Frame ("MulticastChat [" + group.getHostAddress () + ":" + port + "]"); frame.addWindowListener (this); output = new TextArea (); output.setEditable (false); input = new TextField (); input.addActionListener (this); frame.setLayout (new BorderLayout ()); frame.add (output, "Center"); frame.add (input, "South"); frame.pack (); }
2/19/ Method start protected Thread listener; public synchronized void start () throws IOException { if (listener == null) { initNet (); listener = new Thread (this); listener.start (); frame.setVisible (true); } // protected void initNet () throws IOException …
2/19/ Method initNet protected MulticastSocket socket; protected DatagramPacket outgoing, incoming; protected void initNet () throws IOException { socket = new MulticastSocket (port); socket.setTimeToLive (1); socket.joinGroup (group); outgoing = new DatagramPacket (new byte[1], 1, group, port); incoming = new DatagramPacket (new byte[65508], 65508); }
2/19/ Method stop public synchronized void stop () throws IOException { frame.setVisible (false); if (listener != null) { listener.interrupt (); listener = null; try { socket.leaveGroup (group); } finally { socket.close (); }
2/19/ Window-related Methods public void windowOpened (WindowEvent event) { input.requestFocus (); } public void windowClosing (WindowEvent event) { try { stop (); } catch (IOException ex) { ex.printStackTrace (); } public void windowClosed (WindowEvent event) {} public void windowIconified (WindowEvent event) {} public void windowDeiconified (WindowEvent event) {} public void windowActivated (WindowEvent event) {} public void windowDeactivated (WindowEvent event) {}
2/19/ Method actionPerformed public void actionPerformed (ActionEvent event) { try { byte[] utf = event.getActionCommand ().getBytes ("UTF8"); outgoing.setData (utf); outgoing.setLength (utf.length); socket.send (outgoing); input.setText (""); } catch (IOException ex) { handleIOException (ex); } // protected synchronized void handleIOException (IOException ex) …
2/19/ Method handleIOException protected synchronized void handleIOException (IOException ex) { if (listener != null) { output.append (ex + "\n"); input.setVisible (false); frame.validate (); if (listener != Thread.currentThread ()) listener.interrupt (); listener = null; try { socket.leaveGroup (group); } catch (IOException ignored) { } socket.close (); }
2/19/ Method run public void run () { try { while (!Thread.interrupted ()) { incoming.setLength (incoming.getData ().length); socket.receive (incoming); String message = new String (incoming.getData (), 0, incoming.getLength (), "UTF8"); output.append (message + "\n"); } } catch (IOException ex) { handleIOException (ex); }
2/19/ Method main public static void main (String[] args) throws IOException { if ((args.length != 1) || (args[0].indexOf (":") < 0)) throw new IllegalArgumentException ("Syntax: MulticastChat : "); int idx = args[0].indexOf (":"); InetAddress group = InetAddress.getByName (args[0].substring (0, idx)); int port = Integer.parseInt (args[0].substring (idx + 1)); MulticastChat chat = new MulticastChat (group, port); chat.start (); }
2/19/ Next Class IGMP DNS TCP Read TI Ch. 13, 14, 17 Dr. Zachary will give lecture