title: UDP Broadcast in Java
body: <p>Morning.</p>
  
  <p>I'm pretty new in Java and socket connections but I'm trying to send out a UDP packet/broadcast on 255.255.255.255 on port 8001 to a device.  I can get the data to send just fine, however when it comes time to receive the data the connection times out.  I have a packet sniffer and I can see the packet send and then the device respond.</p>
  
  <p>I'm pretty sure it is a rookie mistake that I'm missing in my code but I've been stuck on it for awhile and any help would be appreciated.</p>
  
  <pre><code> m_Socket = new DatagramSocket(m_SERVERPORT);
   InetAddress address = InetAddress.getByName(m_SERVERIP);
  
  
   m_DataPack = new DatagramPacket(m_SERVERCMD.getBytes(), m_SERVERCMD.getBytes().length,
   address, m_SERVERPORT);
   m_Socket.setBroadcast(true);
   m_Socket.connect(address, m_SERVERPORT);
  
   m_Socket.send(m_DataPack);
   m_DataPack = new DatagramPacket(data, data.length,
   address, m_SERVERPORT);
  
  
   m_Socket.receive(m_DataPack); // This is where it times out
  
  
   data = m_DataPack.getData();
   String received = data.toString();
   System.out.println("Received: " + received);
   m_Socket.close();
  </code></pre>
  
  <p>Thanks and Gig'Em.</p>
  
  <p>EDIT:</p>
  
  <p>I'm not sure if this helps but when I watch the m_Socket object I can see the following right before it sends:</p>
  
  <pre><code>bound = true;
  close = false;
  connectedAddress = Inet4Address (id = 32) (-1,-1,-1,-1);
  connectedPort = 8001;
  connectState = 1;
  created = true;
  impl = PlainDatagramSocketImpl;
  oldImpl = false;
  </code></pre>
  
  <p>and the m_DataPack object is the following:</p>
  
  <pre><code>address = Inet4Address (id = 32) (-1,-1,-1,-1);
  bufLength = 6 (size of packet I'm sending is 6 char long);
  offset = 0;
  port = 8001;
  </code></pre>