Android广播UDP循环

时间:2010-09-20 20:22:38

标签: android udp broadcast

我目前正在DatagramSocket上发送一个DatagramPacket,我收到的很好..问题是我收到了我发出的数据包。如果我两次拨打电话,那么它会超时。有没有办法忽略第一个数据包并接收第二个数据包。

这是我的代码..

            socket = new DatagramSocket(8001);
            socket.setBroadcast(true);
            socket.setReuseAddress(false);
            DatagramPacket packet = new DatagramPacket(databytes, 7,
                getBroadcastAddress(), 8001);
            socket.send(packet);
            String localAddress = socket.getLocalAddress().toString();

            byte[] buf = new byte[1024];
            DatagramPacket receivepacket = new DatagramPacket(buf, buf.length);
            socket.setSoTimeout(5000);

            String temp = "";
            String delims = "[/]";
            while(true)
            {
                try{
                    socket.receive(receivepacket);
                    temp = receivepacket.getAddress().toString();
                    temp = temp.split(delims)[0];

                    if(temp != localAddress)
                    {   

                    }else
                    {
                        m_IPAddress = temp;
                        break;
                    }

                }catch (SocketException e){

                } catch (IOException e){
                    String temp1 = e.toString();
                }
            }