0

I want to send/receive messages by the multicast socket. This is my code

'''

public class MulticastSender {

    public static final String GROUP_ADDRESS = "230.0.0.1";
    public static final int PORT = 7766;

    public static void main(String[] args) throws InterruptedException {
        DatagramSocket socket = null;
        try {
            // Get the address that we are going to connect to.
            InetAddress address = InetAddress.getByName(GROUP_ADDRESS);
            System.out.println("GROUP_ADDRESS: " + address);

            // Create a new Multicast socket
            socket = new DatagramSocket();
            
            DatagramPacket outPacket = null;
            long counter = 0;
            while (true) {
                String msg = "Sent message No. " + counter;
                counter++;
                outPacket = new DatagramPacket(msg.getBytes(), msg.getBytes().length, address, PORT);
                socket.send(outPacket);
                System.out.println("Server sent packet with msg: " + msg);
                Thread.sleep(1000); // Sleep 1 second before sending the next message
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (socket != null) {
                socket.close();
            }
        }
    }

'''

'''

public class MulticastReceiver {
 
    public static final byte[] BUFFER = new byte[4096];
 
    public static void main(String[] args) {
        MulticastSocket socket = null;
        DatagramPacket inPacket = null;
        
        try {
            // Get the address that we are going to connect to.
            SocketAddress address = new InetSocketAddress(MulticastSender.GROUP_ADDRESS, MulticastSender.PORT);
           
            // Create a new Multicast socket
            socket = new MulticastSocket(address);            
            socket.joinGroup(address, NetworkInterface.getByName("eth0"));
 
            while (true) {
                // Receive the information and print it.
                inPacket = new DatagramPacket(BUFFER, BUFFER.length);
                socket.receive(inPacket);
//                System.out.println(socket.getInterface());
                String msg = new String(BUFFER, 0, inPacket.getLength());
                System.out.println("From " + inPacket.getAddress() + " Msg : " + msg);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

'''

It is working on my local host. however, I want to send messages from my AWS server instance (with public ipv4) and receive them in the local client. How to do it? if yes, please give me an example! Tks all

  • First, the `230.0.0.1` group is in a RESERVED multicast scope, and you should not use that. If you want to make up your own multicast groups, you should use groups from the Organization-Local scope of `239.0.0.0/8`. Next, you cannot multicast on the public Internet. You would need to use a tunnel that supports multicast, e.g. GRE, and not all tunnels do. Third, if you are trying to route multicast, you must use multicast routing, e.g. PIM, which is very different than unicast routing, and you use IGMP to inform the multicast router that you are interested in receiving data from a group. – Ron Maupin Jun 25 '22 at 18:10
  • Thanks, @RonMaupin, can you share with me documents about it? – Huy Nguyen Jun 28 '22 at 08:22
  • Unfortunately, recommending off-site resources is not allowed here. From the [help/on-topic]: "_Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it._" – Ron Maupin Jun 28 '22 at 12:37

0 Answers0