0

It's my first post so it may be not well stylished but I tried... So... I have two machines, both running Java. I want them to run something like this.

Client: sends multicast to listening servers. Server(s): the server captures the multicast and sends a unicast back with name of local machine that server runs on. Client: receives the unicast with the server adress(es) and makes a list with their hostnames.

But the client doesn't even send the multicast (I was watching wireshark capturing packets) It only sends something when I put 230.0.0.1 as multicast address, but then, the server doesn't receive the packet.

EDIT: When I send a unicast packet to the server it responds fine.

Here is my code:

try
    {
        //The client runs on LeJOS EV3 so I used their classes a bit
        LCDOutputStream lcd = new LCDOutputStream();
        PrintStream p = new PrintStream(lcd);
        while(true)
        {
            if(Button.waitForAnyPress() == Button.ID_ESCAPE)
            {
                break;
            }

            byte[] buf = this.writeString("get_ip");
            DatagramSocket sender = new DatagramSocket();

            DatagramPacket packet = new DatagramPacket(buf, buf.length, InetAddress.getByName("230.0.0.1"), 5555);
            sender.send(packet);
            sender.close();
            p.println("Sent Multicast");
        }
        p.close();
        lcd.close();
    }
    catch(Exception e)
    {
        console.printException(e);
    }

Here is the server code:

MulticastSocket s = new MulticastSocket(5555);
                s.joinGroup(InetAddress.getByName("225.1.1.1"));
                while(true)
                {
                    try
                    {
                        /*
                         * 225.1.100.1
                         *
                        DataSender.Impl.reply("225.1.100.1", 5555, InetAddress.getLocalHost().getHostName(), "get_ip");*/                       
                        byte[] buf = new byte[256];
                        DatagramPacket p = new DatagramPacket(buf, buf.length);
                        s.receive(p);
                        System.out.println("DEBUG: received request");
                    }
                    catch(IOException e)
                    {
                        e.printStackTrace();
                        break;
                    }
                }
                s.close();
Enginecrafter77
  • 103
  • 1
  • 5
  • `while(true)` is **bad code** and will make it deadlock at some point in time - your kernel may skip **any** part of *(everything, even)* your loop body and if that happens your loop will continue forever, putting 100% load on your scheduler - such an application state usually is considered "hanging". Do not try to use "cool tricks" or something like that, code which has no real function automatically is bad and a source of program errors. – specializt Jun 01 '16 at 13:36
  • @specializt Loops do not cause deadlocks. Kernels don't execute Java code, and if they did they would not skip parts of loops. `while(true)` is neither 'bad' nor a 'cool trick' and does have a 'real function'. – user207421 Jun 01 '16 at 15:42
  • @specializt I use the "while(true)" in separate thread to make server respond to packets all the time while the program is alive, not only one time and then ignoring all the other incoming packets. EDIT: I forgot to say when I send an unicast to the server it responds well. – Enginecrafter77 Jun 01 '16 at 17:13
  • @M.H. endless loops are not necessary for long-running threads. At all. You simply can put some sort of variable (like `AtomicBoolean`) into the loop head and stop your threads easily that way ... simply expose the variable via `setters` and `getters` and you're good to go -- thats how actual software is written. Oh and dont follow most of the advices here on SO - quite a few people here are clueless trolls, sockpuppets and the like. Get yourself a good book about programming and study it, that'll teach you much more than most people on SO will be able to tell you. – specializt Jun 02 '16 at 06:14
  • @specializt If you have multiple threads at work, sure you could do that, although you need to make sure there's a timeout configured on the socket so that the condition may be checked periodically. If however you're only working with a single thread, an endless loop is fine. No need to make things more complicated than they need to be. – dbush Jun 02 '16 at 13:05
  • i literally have no clue what you're talking about ... and the statement about `AtomicBoolean` or maybe even `volatile boolean` being "complicated" ... makes no sense whatsoever. Are you mixing up topics, perhaps? – specializt Jun 02 '16 at 16:00

1 Answers1

0

The comments made by ecle in response to the following post helped me resolve a similar issue: Java Multicast sample program is unable to deliver packets within LAN (across hosts). In my case, adding setInterface(<server address>); worked. For example:

MulticastSocket s = new MulticastSocket(5555);
s.setInterface(<server address>);
s.joinGroup(InetAddress.getByName("225.1.1.1"));
Community
  • 1
  • 1
wgw
  • 16
  • I forgot to mention that you should be able to use this in your client code as well, instead of the DatagramSocket. Or, as mentioned in the linked post, you may need to use the constructor `DatagramSocket(int port, InetAddress laddr)` to bind to the correct interface. – wgw Jul 07 '16 at 14:54
  • seems to work when running both on localhost and using your address 225.1.1.1 – Enginecrafter77 Jul 09 '16 at 20:00