简体   繁体   中英

Android Broadcast UDP Looping

I am currently sending out a DatagramPacket on a DatagramSocket and I receive just fine.. the problem is that I am receiving the packet I sent out. If I call the receive twice then it times out. Is there a way to ignore the first packet and receive the second.

Here is my code..

            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();
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM