0

I need to send and receive in multicast.

I posted the question on StackOverflow Forum for checking my code: https://stackoverflow.com/questions/50131973/java-multicast-socket-doesnt-receive-anything-on-windows

I tried my code on Ubuntu and it works perfectly.

Now I'm doing experiments on Windows 10. I use multicast socket to send and receive DatagramPacket. If the InetAddress of datagram is "230.0.0.1" for multicasting, it doesn't work. Otherwise, when I use a LAN connected machine's IP, it works.

Receiver:

    MulticastSocket multiSocket = new MulticastSocket(3575);
    InetAddress groupMulticast = InetAddress.getByName( "230.0.0.1" );
    multiSocket.joinGroup(groupMulticast);
    DatagramPacket packetReceive = new DatagramPacket( bufReceive, bufReceive.length );
    try { 
        multiSocket.receive( packetReceive );
    } catch (IOException e) { e.printStackTrace(); }

Sender:

    MulticastSocket multiSocket = new MulticastSocket(3575);
    InetAddress groupMulticast = InetAddress.getByName( "230.0.0.1" );
    multiSocket.joinGroup(groupMulticast);
    byte[] bufSend = new byte[255];

    DatagramPacket packetSend = new DatagramPacket( bufSend, bufSend.length, 
                                    groupMulticast, 3575 );
    try {
        multiSocket.send(packetSend);
    } catch (IOException e) { e.printStackTrace(); }

This sender doens't work. But if I change this:

InetAddress groupMulticast = InetAddress.getByName( "230.0.0.1" );

with this:

InetAddress groupMulticast = InetAddress.getByName( "192.168.0.21" );

where 192.168.0.21 is IP address of receiver connected in LAN, it works.

Ho can I send in multicasting?

1 Answers1

0

Try this code.

public class Server3 {

public static void main(String[] args) throws IOException {
    MulticastSocket multiSocket = new MulticastSocket(3575);
    InetAddress groupMulticast = InetAddress.getByName("224.0.0.1");
    multiSocket.setBroadcast(true);
    multiSocket.joinGroup(groupMulticast);

    while (true) {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        System.out.println("Sending...");
        String msg = "Hai";
        byte[] bufSend = msg.getBytes();

        DatagramPacket packetSend = new DatagramPacket(bufSend, bufSend.length, groupMulticast, 3575);
        try {
            multiSocket.send(packetSend);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}
public class Client3 {
public static void main(String[] args) throws IOException {
    MulticastSocket multiSocket = new MulticastSocket(3575);
    InetAddress groupMulticast = InetAddress.getByName("224.0.0.1");
    multiSocket.setBroadcast(true);
    multiSocket.joinGroup(groupMulticast);
    byte[] bufReceive = new byte[1024];
    while (true) {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        System.out.println("Receiving...");
        DatagramPacket packetReceive = new DatagramPacket(bufReceive, bufReceive.length);
        try {
            multiSocket.receive(packetReceive);
            System.out.println("msg...");
            System.out.println(new String(bufReceive));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}