Getting `Can't assign requested address` java.net.SocketException using Ehcache multicast

前端 未结 4 740
长情又很酷
长情又很酷 2020-12-23 19:53

Getting java.net.SocketException when trying to start a multicast provider:

2013-09-11 11:45:44,204 [main] ERROR net.sf.ehcache.distribution.Mul         


        
4条回答
  •  忘掉有多难
    2020-12-23 20:46

    You need to add certain configurations to Java VM before you can join a Multicast socket in any machine.

    First add this line before attempting any connection to make sure you will get only IPv4 addresses:

    System.setProperty("java.net.preferIPv4Stack", "true");
    

    In most of the cases your computer has more than one network interface, so you need to choose the correct one:

    Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces();
    while (networkInterfaces.hasMoreElements()) {
        NetworkInterface networkInterface = networkInterfaces.nextElement();
        Enumeration addressesFromNetworkInterface = networkInterface.getInetAddresses();
        while (addressesFromNetworkInterface.hasMoreElements()) {
            InetAddress inetAddress = addressesFromNetworkInterface.nextElement();
            if (inetAddress.isSiteLocalAddress()
                    && !inetAddress.isAnyLocalAddress()
                    && !inetAddress.isLinkLocalAddress()
                    && !inetAddress.isLoopbackAddress()
                    && !inetAddress.isMulticastAddress()) {
                socket.setNetworkInterface(NetworkInterface.getByName(networkInterface.getName()));
            }
        }
    }
    

提交回复
热议问题