Download presentation
Presentation is loading. Please wait.
Published byJustin Parker Modified over 8 years ago
1
CSCE 515: Computer Network Programming Chin-Tser Huang huangct@cse.sc.edu University of South Carolina
2
2/17/20052 Something on Midterm Exam To be held in class on March 3, 2005 (9:30am-10:45am) No excuse will be accepted If any substitute exam is really necessary, it will be MUCH HARDER than original exam
3
2/17/20053 ICMP Source Quench Error type(4)code(0) checksum IP header + first 8 bytes of original datagram data 078151631 unused (must be 0)
4
2/17/20054 Address Bindings of UDP Server Three types of address bindings localIP.lport and foreignIP.fport localIP.lport and *.* *.lport and *.*
5
2/17/20055 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
6
2/17/20056 Broadcasting Four types of IP broadcast addresses Limited broadcast: 255.255.255.255 Net-directed broadcast: e.g. netid.255.255.255 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
7
2/17/20057 Multicasting Two types of services Delivery to multiple destinations Solicitation of servers by clients
8
2/17/20058 Multicast Group Address Combination of high-order 4 bits of “1110” and multicast group ID Range is 224.0.0.0 – 239.255.255.255 1110multicast group ID Class D 28
9
2/17/20059 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 224.0.0.1: all systems on this subnet 224.0.0.2: all routers on this subnet 224.0.1.1: Network Time Protocol (NTP) 224.0.0.9: RIP-2
10
2/17/200510 Converting Multicast Group Addresses to Ethernet Addresses Upper 5 bits of multicast group ID are ignored in the mapping Thus mapping is not unique 1110 0101111000000000100000000 Class D IP address 0781516232431 not used in mapping low-order 23 bits copied to Ethernet address
11
2/17/200511 Class MulticastSocket Extend class DatagramSocket and add support for IP multicast Multiple MulticastSockets can listen to same port on same machine
12
2/17/200512 Class MulticastSocket Constructors MulticastSocket() MulticastSocket(int port)
13
2/17/200513 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 getInterface() throws SocketException
14
2/17/200514 Class MulticastSocket Exceptions IOException SecurityException
15
2/17/200515 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();
16
2/17/200516 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();
17
2/17/200517 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
18
2/17/200518 MulticastChat.java /* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 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 … }
19
2/17/200519 Constructor MulticastChat protected InetAddress group; protected int port; public MulticastChat (InetAddress group, int port) { this.group = group; this.port = port; initAWT (); } // protected void initAWT () …
20
2/17/200520 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 (); }
21
2/17/200521 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 …
22
2/17/200522 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); }
23
2/17/200523 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 (); }
24
2/17/200524 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) {}
25
2/17/200525 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) …
26
2/17/200526 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 (); }
27
2/17/200527 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); }
28
2/17/200528 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 (); }
29
2/17/200529 Next Class IGMP DNS TCP Read TI Ch. 13, 14, 17
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.