Socket listen to many packets without infinite loop
<p>I am writing a java socket program for android:</p>

<p><strong>DeviceManagerWindow.java</strong></p>

<pre><code>public class DeviceManagerWindow extends Activity
{
    Thread sendMulticast;
    ...
    ...
    WifiManager wifi = (WifiManager)getSystemService( Context.WIFI_SERVICE );
    if(wifi != null)
    {
        WifiManager.MulticastLock lock = wifi.createMulticastLock("WifiDevices");
        lock.acquire();
    }
    public void searchDevice(View view)
    {
        sendMulticast = new Thread(new MultiCastThread());
        sendMulticast.start();
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        List&lt;Future&lt;DeviceDetails&gt;&gt; deviceList = new ArrayList&lt;Future&lt;DeviceDetails&gt;&gt;();

            Callable&lt;DeviceDetails&gt; device = new MulticastReceiver();
            Future&lt;DeviceDetails&gt; submit = executorService.submit(device);
            deviceList.add(submit);

        DeviceDetails[] devices = new DeviceDetails[deviceList.size()];
        int i=0;
        for(Future&lt;DeviceDetails&gt; future :deviceList)
        {
            try
            {
                devices[i] = future.get();
            }
            catch(Exception e)
            {
                Log.v("future Exception: ",e.toString());
            }
        }
        listAllDevices(devices);
    }
}
</code></pre>

<p>In the receiver class I have following code:</p>

<p><strong>MultivastReceiver.java</strong></p>

<pre><code>public class MulticastReceiver implements Callable&lt;DeviceDetails&gt;
{
    DatagramSocket socket = null;
    DatagramPacket inPacket = null;
    public MulticastReceiver()
    {
        try
        {
            socket = new DatagramSocket(5500);
        }
        catch(Exception ioe)
        {
            System.out.println(ioe);
        }
    }
    @Override
    public DeviceDetails call() throws Exception
    {
        // TODO Auto-generated method stub
        try
        {
                byte[] inBuf = new byte[WifiConstants.DGRAM_LEN];
                //System.out.println("Listening");
                inPacket = new DatagramPacket(inBuf, inBuf.length);
                socket.receive(inPacket);

                String msg = new String(inBuf, 0, inPacket.getLength());

                Log.v("Received: ","From :" + inPacket.getAddress() + " Msg : " + msg);
                DeviceDetails device = getDeviceFromString(msg);
                return device;
        }
        catch(Exception e)
        {
            Log.v("Receiving Error: ",e.toString());
            return null;
        }
    }
    public DeviceDetails getDeviceFromString(String str)
    {
        String type;
        String IP;
            type=str.substring(0,str.indexOf('`'));
            str = str.substring(str.indexOf('`')+1);
            IP=str;
        DeviceDetails device = new DeviceDetails(type,IP);
        return device;
    }
}
</code></pre>

<p>Now, the above code only searches for the first packet when it receives the first packet it stops searching for the next packet. I want it to listen to the incoming packets for 30 seconds and then quit, with the all the datas that it has received. How to do that.</p>

<p>If I use a infinite loop then it will never return the value, because the return statement will be placed outside the loop and if I put the return statement inside the loop it will return the value and never wait for the next packet to come.</p>
