45

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

2013-09-11 11:45:44,204 [main] ERROR net.sf.ehcache.distribution.MulticastRMICacheManagerPeerProvider: Error starting heartbeat. Error was: Can't assign requested address
java.net.SocketException: Can't assign requested address
at java.net.PlainDatagramSocketImpl.join(Native Method)
at java.net.AbstractPlainDatagramSocketImpl.join(AbstractPlainDatagramSocketImpl.java:178)
at java.net.MulticastSocket.joinGroup(MulticastSocket.java:319)
at net.sf.ehcache.distribution.MulticastKeepaliveHeartbeatReceiver.init(MulticastKeepaliveHeartbeatReceiver.java:88)
at net.sf.ehcache.distribution.MulticastRMICacheManagerPeerProvider.init(MulticastRMICacheManagerPeerProvider.java:95)
eebbesen
  • 5,070
  • 8
  • 48
  • 70

5 Answers5

107

This was caused by an IPv6 address being returned from java.net.NetworkInterface.getDefault(). I'm on a Macbook and was using wireless -- p2p0 (used for AirDrop) was returned as the default network interface but my p2p0 only has an IPv6 ether entry (found by running ipconfig).

Two solutions, both of which worked for me (I prefer the first because it works whether you are using a wired or wireless connection)

  1. Start the JVM with -Djava.net.preferIPv4Stack=true. This caused java.net.NetworkInterface.getDefault() to return my vboxnet0 network interface -- not sure what you'll get if you're not running a host-only VM.
  2. Turn off wireless and use a wired connection
eebbesen
  • 5,070
  • 8
  • 48
  • 70
  • 2
    Thanks, couldnt get mulitcast working on my macbook until I tired this. – Loran Mar 05 '15 at 01:50
  • Glad to hear it @Loran! Which solution did you go with? – eebbesen Mar 05 '15 at 04:20
  • 5
    `java -Djava.net.preferIPv4Stack=true MyProgram` – Loran Mar 11 '15 at 20:35
  • @Loran that's the best of the two, imho – eebbesen Mar 11 '15 at 23:19
  • 2
    Here's how to set those properties if using maven: http://stackoverflow.com/a/7579853/32453 – rogerdpack Jun 11 '15 at 19:41
  • 1
    Awesome solution!! finally find this after struggling one day in debugging!! More ppl should see. – JohnnyHuo Sep 17 '15 at 17:56
  • Glad it worked for you @JohnnyHuo! If you upvote it more people will see it :). – eebbesen Sep 17 '15 at 18:43
  • 1
    For those of you who wonder how to set -Djava.net.preferIPv4Stack=true in Eclipse: Right click your project and select "Run As", then select "Run Configurations...". Append "-Djava.net.preferIPv4Stack=true" into VM arguments and click "Apply". – JohnnyHuo Sep 17 '15 at 19:08
  • 1
    If you are using JBOSS AMQ Clustering and end up with the same error on a mac, add the '-Djava.net.preferIPv4Stack=true' entry to JAVA_ARGS in 'artemis.profile' in AMQ_HOME/instances/YOUR_INSTANCE/etc folder. – Sri Jun 13 '17 at 07:59
  • 1
    this was helpful for me – dcrearer Sep 11 '17 at 12:26
  • How and where is this configuration done please, none of the answers say this? – Jude Ukana Jun 06 '22 at 10:28
  • you can set "Djava.net.preferIPv4Stack=true" from the terminal at the point of deploying to a server. In my Case/example below i am deploying to a Payara Micro server. 'java -Djava.net.preferIPv4Stack=true -jar pm.jar --deploy /Users/jukana/NetBeansProjects/hello-todo/target/hello-todo.war --port 8080' – Jude Ukana Jun 06 '22 at 11:37
11

A slight variation on the accepted answer: You can also add the following line of code to your java code:

System.setProperty("java.net.preferIPv4Stack", "true");
user3697700
  • 145
  • 1
  • 7
  • 4
    This does not always work; at least under macOS, the "java.net.preferIPv4Stack"-property is only read at JVM startup – setting it programmatically has no effect. – K. Biermann Nov 24 '16 at 11:42
  • @K. Biermann there are different ways to manage the applications configuration data with properties and the one user3697700 showed us works fine. See https://stackoverflow.com/questions/39188826/what-are-custom-jvm-properties/39189064 and https://docs.oracle.com/javase/tutorial/essential/environment/properties.html – Daniel B Dec 28 '19 at 21:54
7

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<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
    NetworkInterface networkInterface = networkInterfaces.nextElement();
    Enumeration<InetAddress> 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()));
        }
    }
}
StefMa
  • 3,344
  • 4
  • 27
  • 48
Camilo Ortegón
  • 3,414
  • 3
  • 26
  • 34
0

In my case I had just began using a VPN to a network that required authentication. My app would start and could connect to its databases through the pipe fine but my configuration for distributed cache using the IP 230.0.0.1 in ehcach.xml was the cause. In production all was well, locally it would simply fail and rollback to a different strategy but via the VPN the multicast requests were met with an authentication challenge and this error was the result. I only required a short term fix so in these environments I disable the ehcache multicast configuration and things returned to normal.

This was the offending line in ehcache.xml which was simply commented out

<cacheManagerPeerProviderFactory
  class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
  properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446, timeToLive=32"
/>
eebbesen
  • 5,070
  • 8
  • 48
  • 70
0

to add to eebbesen's answer (the most correct/voted answer), you can set "Djava.net.preferIPv4Stack=true" from the terminal at the point of deploying to a server.

In my Case/example below i am deploying to a Payara Micro server. 'java -Djava.net.preferIPv4Stack=true -jar pm.jar --deploy /Users/jukana/NetBeansProjects/hello-todo/target/hello-todo.war --port 8080'

Jude Ukana
  • 121
  • 7