java.net.NoRouteToHostException: No route to host: no further information

608 views Asked by At

I tried to run a multicast and this error appeared:

java.net.NoRouteToHostException: No route to host: no further information
            at java.base/sun.nio.ch.DatagramChannelImpl.send0(Native Method)
            at java.base/sun.nio.ch.DatagramChannelImpl.sendFromNativeBuffer(DatagramChannelImpl.java:897)
            at java.base/sun.nio.ch.DatagramChannelImpl.send(DatagramChannelImpl.java:859)
            at java.base/sun.nio.ch.DatagramChannelImpl.send(DatagramChannelImpl.java:817)
            at java.base/sun.nio.ch.DatagramChannelImpl.blockingSend(DatagramChannelImpl.java:849)        
            at java.base/sun.nio.ch.DatagramSocketAdaptor.send(DatagramSocketAdaptor.java:217)
            at java.base/java.net.DatagramSocket.send(DatagramSocket.java:531)
            at sockets.MulticastServer.run(MulticastServer.java:33)

Source code:

    package sockets;
import java.net.MulticastSocket;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.io.IOException;

public class MulticastServer extends Thread {
    private String MULTICAST_ADDRESS = "224.3.2.1";
    private int PORT = 4321;
    private long SLEEP_TIME = 5000;

    public static void main(String[] args) {
        MulticastServer server = new MulticastServer();
        server.start();
    }

    public MulticastServer() {
        super("Server " + (long) (Math.random() * 1000));
    }

    public void run() {
        MulticastSocket socket = null;
        long counter = 0;
        System.out.println(this.getName() + " running...");
        try {
            socket = new MulticastSocket();  // create socket without binding it (only for sending)
            while (true) {
                String message = this.getName() + " packet " + counter++;
                byte[] buffer = message.getBytes();

                InetAddress group = InetAddress.getByName(MULTICAST_ADDRESS);
                DatagramPacket packet = new DatagramPacket(buffer, buffer.length, group, PORT);
                socket.send(packet);

                try { sleep((long) (Math.random() * SLEEP_TIME)); } catch (InterruptedException e) { }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            socket.close();
        }
    }
}

i tried changing the port

0

There are 0 answers