title: Java MulticastSocket. How to stop/pause the "receive" method
body: <p>how do I stop this method from executing?</p>
  
  <pre><code>multSocket.receive(multPacket);    
  </code></pre>
  
  <p>I'm trying to connect two or more machines (or applications on the same machine) via UDP Multicast with Java. The thing is I'm not working with client-server but more like a distributed object, so there isn't really a distinction between clients and servers.</p>
  
  <p>Ideally the first application opened would start sending multicast messages every x seconds, and when the other one gets started would call a method for receiving the message or packet. But since each application can be client or server it's tricky to know which one to put to send and the other to receive.</p>
  
  <p>My current idea for fixing this is to make each application send the multicast packet for 10 seconds, if no one responds then listen to multicast packets for 10 seconds, if it doesn't receive anything start sending again and keep the cycle. When one of them finally receives the message, send a confirmation to the sender and both of them can stop listening and sending.</p>
  
  <p>But my problem is the receiving method. It stays listening for incoming multicast packets and I don't know how to stop or pause it.</p>
  
  <p>This is how I init the socket just in case</p>
  
  <pre><code>MulticastSocket multSocket = new MulticastSocket(5001);
  multSocket.joinGroup(230.0.0.1);
  private byte[] multBuf = new byte[256];
  DatagramPacket multPacket = new DatagramPacket(multBuf, multBuf.length);
  </code></pre>
  
  <p>Thanks for hearing me out.</p>
  
  <p>-------------------------------------------------------------- EDIT --------------------------------------------------------------</p>
  
  <p>-------------------------------------------------------------- EDIT --------------------------------------------------------------</p>
  
  <p>@EJP Thanks for your answer! This is how I implemented the setSoTimeout on the receive multicast method:</p>
  
  <pre><code>public void receiveMulticast() throws IOException{
  
      multAddress = InetAddress.getByName("230.0.0.1");
      multSocket = new MulticastSocket(multPort);
      multSocket.joinGroup(multAddress);
      multBuf = new byte[256];
      multPacket = new DatagramPacket(multBuf, multBuf.length);
  
      multSocket.setSoTimeout(10000);
  
      try {
          // This is the sentence I had trouble with
          multSocket.receive(multPacket);
  
          receivedPacket = true;
          System.out.println("Received something");
  
      } catch (SocketTimeoutException s) {
          System.out.println("Socket timed out!");
      }
  
  }
  </code></pre>
  
  <p>Here's more of the code in case someone's curious:</p>
  
  <p>This is my send multicast method which uses sleep for sending every 10 seconds:</p>
  
  <pre><code>public void sendMulticast() throws IOException, InterruptedException{
  
      multAddress = InetAddress.getByName("230.0.0.1");
      multSocket = new MulticastSocket();
      multSocket.joinGroup(multAddress);
      int tcpPort = 5001;
      String data = String.valueOf(tcpPort);
      multBuf = data.getBytes();
      multPacket = new DatagramPacket(multBuf, multBuf.length, multAddress, multPort);
  
      int count = 0;
      while (count &lt; 10) {
          Thread.sleep(1000);
          multSocket.send(multPacket);
          count++;
      }
  
  }
  </code></pre>
  
  <p>And this is where I call those two methods infinitetly (on another class):</p>
  
  <pre><code>messageThread = new Thread() {
      @Override
      public void run() {
          while(true){
              try {
                  stateLabel.setText("Sending");
                  socket.sendMulticast();
                  stateLabel.setText("Listening");
                  socket.receiveMulticast();
              } catch (IOException ex) {
                  Logger.getLogger(NumerosAmigosView.class.getName()).log(Level.SEVERE, null, ex);
              } catch (InterruptedException ex) {
                  Logger.getLogger(NumerosAmigosView.class.getName()).log(Level.SEVERE, null, ex);
              }
          }
      }
  };
  messageThread.start();
  </code></pre>
  
  <p>So the result is the program runs infinitely sending a packet for 10 seconds, receiving for 10 seconds and starting over.
  When I have two programs running at the same time, the packets get received :).</p>