title: java Peer to Peer using UDP socket
body: <p>I am having a simple Peer to Peer program written in java. I have the following code.</p>
  
  <pre><code>import java.net.*;
  import java.io.*;
  public class Broadcasts {
  
  private final Runnable receiver;
  private final Runnable sender;
  private boolean run = true;
  
  
    public Broadcasts(UDPSocketHandler parent)  throws UnknownHostException{
      InetAddress aHost = InetAddress.getLocalHost();
  
      sender = new Runnable() {
          public void run() {
              byte data[] = "Hello".getBytes();
              DatagramSocket socket = null;
              try {
                  socket = new DatagramSocket();
              } catch (SocketException ex) {
                  ex.printStackTrace();
                  parent.quit();
              }
  
              DatagramPacket packet = new DatagramPacket(
                      data,
                      data.length,
                      aHost,
                      9090);
              while (run) {
                  try {
  
                      System.out.println("what is sent"+new String(packet.getData()));
                      socket.send(packet);
                      Thread.sleep(1000);
                  } catch (IOException ex) {
                      ex.printStackTrace();
                      parent.quit();
                  } catch (InterruptedException ex) {
                      ex.printStackTrace();
                      parent.quit();
                  }
              }
          }
      };
  
  
      receiver = new Runnable() {
          public void run() {
              byte data[] = new byte[0];
              DatagramSocket socket = null;
              try {
                  socket = new DatagramSocket(9090);
              } catch (SocketException ex) {
                  ex.printStackTrace();
                  //parent.quit();
              }
              DatagramPacket packet = new DatagramPacket(data, data.length);
              //System.out.println("this is what has been received"+packet.getData());
              String temp;
             // while (run) {
                  try {
                      socket.receive(packet);
                      System.out.println("this is what has been received"+packet.getData());
  
                      //System.out.println("Message received ..."+ temp);
                  } catch (IOException ex) {
                      ex.printStackTrace();
                      parent.quit();
                  }
  
              //}
          }
      };
  
  
      new Thread(sender).start();
      new Thread(receiver).start();
  
  }
  
    public void quit() {
      run = false;
    }
  }
  </code></pre>
  
  <p>Then I have the following class to handle my communication</p>
  
  <pre><code>public class UDPSocketHandler {
  
    private final Broadcasts broadcasts;
  // continue running application?
    private boolean run = true;
  
    public UDPSocketHandler() throws UnknownHostException
    {
      // start socket server to accept incoming connections
      new Thread().start();
  
  
      // late initialize of UDP broadcast and receive, to ensure needed
      // objects are instantiated
      broadcasts = new Broadcasts(this);
  }
  
  // global quit method shuts down everything and exits
  public void quit() {
      run = false;
      broadcasts.quit();
      System.exit(0);
  }
  
  // application entry
    public static void main(String[] args) throws UnknownHostException{
     new UDPSocketHandler();
  
    }
  }
  </code></pre>
  
  <p>The problem is that the receiver do not receive anything.  From what I understood, we could run the sender and receive on the same program as shown in this <a href="https://stackoverflow.com/questions/10069059/is-it-possible-to-run-a-socket-server-and-socket-client-on-the-same-machine">question</a>. That is actually what I would like to do but using UDP instead of TCP. What is the problem with my code?</p>