



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Public class MulticastSocket extends DatagramSocket
MulticastSocket ( ) throws SocketException MulticastSocket ( int portNum ) throws SocketException
public void joinGroup(inetAddress mAddr) throws IOexception
used to join a multicast group using the address, mAddr. Here is an example:
import java.net.; import java.io.; public class MulticastJoin { public static void main(String [ ] args){ try { MulticastSocket mSocket = new MulticastSocket(4001); InetAddress mAddr = InetAddress.getByName("224.0.0.1"); mSocket.joinGroup(mAddr); byte [ ] buffer = new byte[512]; while (true) { DatagramPacket dp = new DatagramPacket(buffer, buffer.length); mSocket.receive(dp); String str = new String(dp.getData(), "8859_1"); System.out.println(str); }//end of while }//end of try catch (SocketException se){ System.out.println("Socket Exception : " + se); } catch (IOException e) { System.out.println("Exception : " + e); } }//end of main }// end of class definition
public void setInterface(InetAddress addr) throws SocketException
used to select the network interface to be used for multicasting in a multihomed system.
public InetAddress getInterface() throws SocketException
used to determine the address of the interface involved in multicasting.
public void setTimeToLive(int TTL) throws IOException
public int getTimeToLive( ) throws IOException sets/gets the time-to-live value of the datagram packet.
Here is one final example of multicasting:
import java.net.; import java.io.; public class MulticastListener { public static void main( String [ ] args) { InetAddress mAddr=null; MulticastSocket mSocket=null; final int PORT_NUM= 4001; try { mAddr = InetAddress.getByName("audionews.mcast.net"); mSocket = new MulticastSocket(PORT_NUM); String hostname = InetAddress.getLocalHost().getHostName(); byte [ ] buffer = new byte[8192]; mSocket.joinGroup(mAddr); System.out.println("Listening from " + hostname + " at " + mAddr.getHostName()); while (true){ DatagramPacket dp = new DatagramPacket(buffer, buffer.length); mSocket.receive(dp); String str = new String(dp.getData(), "8859_1"); System.out.println(str); }//end of while } catch (SocketException se) { System.out.println("Socket Exception : " + se); } catch (IOException e) { System.out.println("Exception : " + e); } finally { if (mSocket != null){ try { mSocket.leaveGroup(mAddr); mSocket.close(); } catch (IOException e){ } }//end of if }//end of finally
}//end of main }