/* * MCServer.java * * Created on 18 luty 2008, 09:30 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package sclient; /** * * @author */ // MCServer.java import java.io.*; import java.net.*; class MCServer { public static void main (String[] args) throws IOException { System.out.println ("Server starting...\n"); // Create a MulticastSocket not bound to any port. MulticastSocket s = new MulticastSocket (); // Because MulticastSocket subclasses DatagramSocket, it is // legal to replace MulticastSocket s = new MulticastSocket (); // with the following line. // DatagramSocket s = new DatagramSocket (); // Obtain an InetAddress object that contains the multicast // group address 231.0.0.1. The InetAddress object is used by // DatagramPacket. InetAddress group = InetAddress.getByName ("231.0.0.1"); // Create a DatagramPacket object that encapsulates a reference // to a byte array (later) and destination address // information. The destination address consists of the // multicast group address (as stored in the InetAddress object) // and port number 10000 -- the port to which multicast datagram // packets are sent. (Note: The dummy array is used to prevent a // NullPointerException object being thrown from the // DatagramPacket constructor.) byte [] dummy = new byte [0]; DatagramPacket dgp = new DatagramPacket (dummy, 0, group, 10000); // Send 30000 Strings to the port. for (int i = 0; i < 30000; i++) { // Create an array of bytes from a String. The platform's // default character set is used to convert from Unicode // characters to bytes. byte [] buffer = ("Video line " + i).getBytes (); // Establish the byte array as the datagram packet's // buffer. dgp.setData (buffer); // Establish the byte array's length as the length of the // datagram packet's buffer. dgp.setLength (buffer.length); // Send the datagram to all members of the multicast group // that listen on port 10000. s.send (dgp); } // Close the socket. s.close (); } }
/* * MCClient.java * * Created on 18 luty 2008, 09:28 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package sclient; /** * * @author */ // MCClient.java import java.io.*; import java.net.*; class MCClient { public static void main (String [] args) throws IOException { // Create a MulticastSocket bound to local port 10000. All // multicast packets from the server program are received // on that port. MulticastSocket s = new MulticastSocket (10000); // Obtain an InetAddress object that contains the multicast // group address 231.0.0.1. The InetAddress object is used by // DatagramPacket. InetAddress group = InetAddress.getByName ("231.0.0.1"); // Join the multicast group so that datagram packets can be // received. s.joinGroup (group); // Read several datagram packets from the server program. for (int i = 0; i < 10; i++) { // No line will exceed 256 bytes. byte [] buffer = new byte [256]; // The DatagramPacket object needs no addressing // information because the socket contains the address. DatagramPacket dgp = new DatagramPacket (buffer, buffer.length); // Receive a datagram packet. s.receive (dgp); // Create a second byte array with a length that matches // the length of the sent data. byte [] buffer2 = new byte [dgp.getLength ()]; // Copy the sent data to the second byte array. System.arraycopy (dgp.getData (), 0, buffer2, 0, dgp.getLength ()); // Print the contents of the second byte array. (Try // printing the contents of buffer. You will soon see why // buffer2 is used.) System.out.println (new String (buffer2)); } // Leave the multicast group. s.leaveGroup (group); // Close the socket. s.close (); } }