Skip to content

native-image: new MulticastSocket(port) fails when multiple sockets are created #1662

@belaban

Description

@belaban

Calling the MulticastSocket(int port) constructor multiple times (with the same port argument) in the same process fails (this is supposed to work).

Calling the MulticastSocket(SocketAddress a) constructor with a non-null InetAddress in InetSocketAddress works.

Alternatively, the sample program in the Javadoc for MulticastSocket [1] can be run twice (add a little sleep); the second process will fail.

[1] https://docs.oracle.com/javase/8/docs/api/java/net/MulticastSocket.html

Sample program to reproduce:

public class MulticastTest2 {

    protected static final InetAddress MCAST_ADDR, BIND_ADDR;
    protected static final int MCAST_PORT=7600;

    static {
        try {
            MCAST_ADDR=InetAddress.getByName("228.8.8.8");
            BIND_ADDR=InetAddress.getLocalHost();
        }
        catch(UnknownHostException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws Exception {
        MulticastSocket sock1=create();
        MulticastSocket sock2=create(); // this works under GraalVM 19.2.0
        System.out.printf("sock1: %s\nsock2: %s\n", sock1.getLocalSocketAddress(), sock2.getLocalSocketAddress());
        Util.close(sock1, sock2);
        sock1=new MulticastSocket(MCAST_PORT);
        sock2=new MulticastSocket(MCAST_PORT);  // this fails under GraalVM 19.2.0
    }

    protected static MulticastSocket create() throws IOException {
        MulticastSocket sock=null;
        SocketAddress saddr=new InetSocketAddress(MCAST_ADDR, MCAST_PORT);
        sock=new MulticastSocket(saddr);
        if(BIND_ADDR != null)
            sock.setInterface(BIND_ADDR);
        return sock;
    }
}

Error:

Exception in thread "main" java.net.BindException: Address already in use
	at java.net.PlainDatagramSocketImpl.bind0(PlainDatagramSocketImpl.java:250)
	at java.net.AbstractPlainDatagramSocketImpl.bind(AbstractPlainDatagramSocketImpl.java:93)
	at java.net.DatagramSocket.bind(DatagramSocket.java:392)
	at java.net.MulticastSocket.<init>(MulticastSocket.java:172)
	at java.net.MulticastSocket.<init>(MulticastSocket.java:136)
	at org.jgroups.tests.MulticastTest2.main(MulticastTest2.java:36)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions