diff src/main/java/jp/ac/u_ryukyu/treevnc/TreeRootFinderListener.java @ 434:aad75cd6d9e2

Multicast joins both ipv4 and ipv6 now.
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 11 Jun 2016 22:42:58 +0900
parents 42fcc9419498
children 4ac83ab4be81
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/treevnc/TreeRootFinderListener.java	Fri Jun 10 19:58:59 2016 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/treevnc/TreeRootFinderListener.java	Sat Jun 11 22:42:58 2016 +0900
@@ -1,16 +1,15 @@
 package jp.ac.u_ryukyu.treevnc;
 
-import java.io.IOException;
-import java.net.*;
-import java.lang.SecurityManager;
-
 import com.glavsoft.rfb.protocol.ProtocolContext.TreeCommand;
 import com.glavsoft.viewer.ViewerInterface;
 import com.glavsoft.viewer.swing.ConnectionParams;
 
+import java.io.IOException;
+import java.net.*;
+
 public class TreeRootFinderListener implements Runnable {
     public static final String Ipv4McastAddr = "224.0.0.1";
-    public static final String Ipv6McastAddr = "FF02::1";
+    public static final String Ipv6McastAddr = "ff02::1";
     public static String McastAddr = Ipv4McastAddr;
 
 	static final int BufSize = 1024;
@@ -22,21 +21,31 @@
 	public TreeRootFinderListener(ViewerInterface vncProxyService) {
 		vps = vncProxyService;
 	}
-	
-	/**
+
+    public static MulticastSocket createMulticastSocket() throws IOException {
+        MulticastSocket soc = new MulticastSocket(ConnectionParams.DEFAULT_VNC_ROOT_FINDER);
+		try {
+			soc.joinGroup(InetAddress.getByName(McastAddr));
+		} catch (SocketException e) {
+			System.out.println("join to " +  Ipv4McastAddr + " failed.");
+		}
+		try {
+			soc.joinGroup(InetAddress.getByName(Ipv6McastAddr));
+		} catch (SocketException e) {
+			System.out.println("join to " +  Ipv6McastAddr + " failed.");
+		}
+        return soc;
+    }
+
+    /**
 	 * To find TreeVNC root, a client sends me a multicast, reply our address to him.
 	 *  It contains a port to receive, so multiple TREEVNC clients can run on a PC. 
 	 */
 	private void replyToRootSearchMulticast() {
 		byte[] buf = new byte[BufSize];
 		try {
-			soc = new MulticastSocket(ConnectionParams.DEFAULT_VNC_ROOT_FINDER);
-            try {
-                soc.joinGroup(InetAddress.getByName(McastAddr));
-            } catch (SocketException e) {
-                McastAddr = Ipv6McastAddr;
-                soc.joinGroup(InetAddress.getByName(Ipv6McastAddr));
-            }
+            soc = createMulticastSocket();
+			System.out.println("FindRoot listening on "+ InetAddress.getByName(McastAddr));
 
             DatagramPacket recvPacket = new DatagramPacket(buf, BufSize);
             while (!stopFlag) {
@@ -67,8 +76,8 @@
             System.out.println("tree-root-find-listener :" + e.getMessage());
 		}
 	}
-	
-	public int parse_code( byte[] bs,int offset,int len )
+
+    public int parse_code( byte[] bs,int offset,int len )
 	{
 	    int intval = 0;
 	    for( int i = offset; i < len ; i++ ) {