title: Multicast packet not received by MulticastSocket on any localhost client
body: <p><strong>Intro</strong></p>
  
  <p>There are a number of tutorials on creating a multicast publisher and receiver. </p>
  
  <p>I used the one found <a href="https://docs.oracle.com/javase/tutorial/networking/datagrams/broadcasting.html" rel="nofollow noreferrer">here</a> with a few modifications.</p>
  
  <p>A few others: </p>
  
  <ul>
  <li><a href="http://www.java2s.com/Tutorials/Java/Java_Network/0050__Java_Network_UDP_Multicast.htm" rel="nofollow noreferrer">here</a></li>
  <li><a href="http://staff.www.ltu.se/~peppar/java/multicast_example/" rel="nofollow noreferrer">here</a> </li>
  <li><a href="https://examples.javacodegeeks.com/core-java/net/multicastsocket-net/java-net-multicastsocket-example/" rel="nofollow noreferrer">here</a></li>
  </ul>
  
  <p>note that these tutorials are all quite similar.</p>
  
  <p><strong>Details:</strong></p>
  
  <p>The server runs on port <code>7777</code> and sends datagrams to <code>224.0.0.0</code> (I tested a couple of other ip's in the multicast range: <code>224.0.0.0</code> to <code>239.255.255.255</code>, but these didn't work) </p>
  
  <p>Client then joins the multicast group <code>224.0.0.0</code> and waits for a packet response (run as a thread)</p>
  
  <p><em>extra:</em> I send a message like: <code>123.23.13.12[host-name]:1234</code> as the datagram data.</p>
  
  <p><strong>Problem:</strong></p>
  
  <p>Multicast packets from server (on localhost) not reaching client (on localhost).</p>
  
  <p>Clients include a java console application (code found below) and Android application on Android Emulator. Both clients do not receive multicast packets.</p>
  
  <p>I know that the multicast packets are being sent as this is shown in <a href="https://en.wikiversity.org/wiki/Wireshark/IPv4_multicast" rel="nofollow noreferrer">Wireshark</a></p>
  
  <p>Below you will find a basic example of that which I have. </p>
  
  <p><strong>TL;DR</strong>: Server sends multicast packets (confirmed via Wireshark) but client doesn't receive them.</p>
  
  <p>Suggestions are very welcome!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Based on  <code>Just another Java programmer</code>'s comment, I check my firewall. Lo and behold, my firewall was dropping on the <code>input</code> and <code>forward</code> chains. I set this to <code>accept</code>all incoming (temporarily)</p>
  
  <p>Based on <code>Ron Maupin</code>'s comments. </p>
  
  <ul>
  <li>I have changed the message sent to exclude the hostname, thus the message sent is <code>123.12.13.23:1234</code></li>
  <li>I have changed the multicast send address to <code>239.254.0.0</code> which is within the specified range (see Ron's comment)</li>
  <li>the multicast port is set to <code>7777</code></li>
  <li>the outgoing interface is set with <code>s.setInterface(InetAddress.getLocalHost())</code> in the <code>broadcastServer()</code> <code>try catch</code> block</li>
  </ul>
  
  <p>With these changes applied, the client(s) still do not receive any packets.</p>
  
  <p><strong>Code:</strong></p>
  
  <p>Server Side (Console App):</p>
  
  <pre><code>String multicastAddress = "239.254.0.0", multicastPort = 7777;
  
  private void broadcastServer() {
      String message = null;
      MulticastSocket s = null;
      InetAddress local = null, group = null;
      InetAddress[] allByName;
      try {
          local = InetAddress.getLocalHost();
          s = new MulticastSocket(multicastPort);
          s.setReuseAddress(true);
          s.setInterface(local)
          s.joinGroup(InetAddress.getByName(multicastAddress));
          group = InetAddress.getByName(multicastAddress);
      } catch (SocketException e) {
          e.printStackTrace();
      } catch (UnknownHostException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
  
      MulticastSocket socket = s;
  
      // getNetworkIP() gets lan network ip
      // serverport = 1025
      message = local.getHostAddress() + ":" + String.valueOf(serverPort);
      byte[] buf = message.getBytes();
      DatagramPacket packet = new DatagramPacket(buf, buf.length, group, multicastPort);
  
      thdBroadcaster = new Thread(() -&gt; {
          while (bRunServer) {
              try {
                  Thread.sleep(1000);
                  printf("[Broadcast] Broadcasting...");
                  socket.send(packet);
                  printf("OK\n");
                  printf("");
              } catch (IOException e) {
                  e.printStackTrace();
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
          socket.close();
      });
      thdBroadcaster.start();
  }
  </code></pre>
  
  <p>Client Side (Console app):</p>
  
  <pre><code>String multicastAddress = "239.254.0.0", multicastPort = 7777;
  
  private void startServerListenerThread() {
      Thread thdServerListener = new Thread(new Runnable() {
          @Override
          public void run() {
              MulticastSocket socket = null;
              InetAddress group = null;
              try {
                  socket = new MulticastSocket(multicastPort);
                  socket.setReuseAddress(true);
                  group = InetAddress.getByName(multicastAddress);
                  socket.joinGroup(group);
  
                  handleServerBroadcasts(socket);
                  socket.leaveGroup(group);
                  socket.close();
  
              } catch (UnknownHostException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
  
          private void handleServerBroadcasts(final MulticastSocket socket) {
              while (true){
                  try {
                      byte[] buf = new byte[256];
                      DatagramPacket packet = new DatagramPacket(buf, buf.length);
                      socket.receive(packet);
                      String received = new String(packet.getData());
  
                      String address = received.substring(0, received.indexOf(":"));
                      String port = received.substring(received.indexOf(":") + 1);
  
                      System.out.println();
                  } catch (IOException e) {
                      e.printStackTrace();
                  } catch (Exception x){
                      x.printStackTrace();
                  }
              }
          }
      });
      thdServerListener.start();
  }
  </code></pre>