/Users/lyon/j4p/src/net/multicast/Utils.java

1    package net.multicast; 
2     
3    import java.net.MulticastSocket; 
4    import java.net.InetAddress; 
5    import java.net.UnknownHostException; 
6    import java.io.IOException; 
7     
8    /** 
9     * Copyright DocJava, inc. User: lyon 
10    * <p/> 
11    * Date: Nov 24, 2004 
12    * <p/> 
13    * Time: 6:30:54 PM 
14    */ 
15   public class Utils { 
16       public static MulticastSocket getMulticastSocket(int port, 
17                                                        InetAddress inetAddress) 
18               throws IOException { 
19    
20           MulticastSocket mcastSocket = new MulticastSocket(port); 
21           mcastSocket.setReuseAddress(true); 
22           mcastSocket.setLoopbackMode(false); 
23           System.out.println("loopback="+mcastSocket.getLoopbackMode()); 
24           mcastSocket.setTimeToLive(2); 
25           // This enables me to see stuff in the group.... 
26           mcastSocket.joinGroup(inetAddress); 
27    
28           return mcastSocket; 
29       } 
30    
31    
32    
33       public static InetAddress getInetAddress() 
34               throws UnknownHostException { 
35           InetAddress inetAddress = InetAddress.getByName("224.111.112.113"); 
36           if (!inetAddress.isMulticastAddress()) { 
37               System.out.println("The address: " + 
38                       inetAddress + 
39                       " is not multicast address"); 
40               System.exit(0); 
41           } 
42           return inetAddress; 
43       } 
44    
45       public static void closeSocket(MulticastSocket mcastSocket, 
46                                      InetAddress inetAddress) 
47               throws IOException { 
48           mcastSocket.leaveGroup(inetAddress); 
49           mcastSocket.close(); 
50       } 
51   } 
52