Listening datagram socket with any ip and any port number android
<p>Hi Am sending broadcast using DatagramSocket with specific port number. Its successfully send message. But when i listening for receive message with open ip address and any port number it throwing sockettimeoutexception. But am connected with the same network.</p>

<p>//manifest</p>

<pre><code>&lt;uses-permission android:name="android.permission.INTERNET" /&gt;
&lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
&lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
&lt;uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /&gt;
&lt;uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /&gt;
&lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
</code></pre>

<p>//defined port number globally</p>

<pre><code>int PORT=2739;
</code></pre>

<p>// starting async task</p>

<pre><code>new MyClientTask().execute();
</code></pre>

<p>///async task</p>

<pre><code>public class MyClientTask extends AsyncTask&lt;Void, Void, Void&gt; {

        @Override
        protected Void doInBackground(Void... arg0) {

            new PacketSender().run();
            new PacketReceiver().run();

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);


        }

    }
</code></pre>

<p>This is my sending request</p>

<pre><code>public class PacketSender implements Runnable {
        @Override
        public void run() {
            DatagramSocket sendSoc = null;
            try {
                sendSoc = new DatagramSocket();
                sendSoc.setBroadcast(true);
                sendSoc.setSoTimeout(5000);
                sendSoc.setReuseAddress(true);
                byte[] ip = prop.ToBuffer();
                DatagramPacket packet = new DatagramPacket(ip,
                        ip.length, getBroadcastIp(), PORT);
                Log.d("Sending", packet.getAddress().getHostAddress() + " " + packet.getPort());
                sendSoc.send(packet);
                sendSoc.close();
            } catch (IOException e) {
                Log.d("Send", "IOException");
                e.printStackTrace();
                if (sendSoc != null)
                    sendSoc.close();
            }
        }
    }
</code></pre>

<p>This is my listening request</p>

<pre><code>public class PacketReceiver implements Runnable {
        @Override
        public void run() {
            DatagramSocket receiveSoc = null;
            try {
              //here 0 represent any port number including system reserved port number
                receiveSoc = new DatagramSocket(0, InetAddress.getByName("0.0.0.0"));  
                receiveSoc.setBroadcast(false); //also tried with true
                receiveSoc.setSoTimeout(5000);  //alse tried with removing timeout value

                int i = 0;
                while (true) {
                    Log.d("data value", " " + i);
                    i++;
                    byte buf[] = new byte[1024];
                    DatagramPacket pack = new DatagramPacket(buf, buf.length);

                    try {
                        receiveSoc.receive(pack);

                        String message = new String(pack.getData()).trim();
                        Log.i("test", message + " server ip : " + pack.getAddress().getHostAddress());

                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                }
            } catch (SocketTimeoutException e) {
                Log.d("Main", "SocketTimeoutException");
                e.printStackTrace();
                receiveSoc.close();
                new PacketReceiver().run();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.d("Response", "IOException");
                e.printStackTrace();
            }
        }
    }
</code></pre>

<p>//getting broadcast ip</p>

<pre><code>public InetAddress getBroadcastIp() throws IOException {
        DhcpInfo dhcp = myWifiManager.getDhcpInfo();
        int broadcast = (dhcp.ipAddress &amp; dhcp.netmask) | ~dhcp.netmask;
        byte[] quads = new byte[4];
        for (int k = 0; k &lt; 4; k++)
            quads[k] = (byte) ((broadcast &gt;&gt; k * 8) &amp; 0xFF);
        return InetAddress.getByAddress(quads);
    }
</code></pre>

<p>It can able to receive broadcast message from all host in LAN except the hardware which i want to communicate. I can send broadcast to it.But i cant able to receive broadcast from this particular device. </p>