开发者

Android Broadcast UDP Looping

开发者 https://www.devze.com 2023-01-17 02:07 出处:网络
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 ther

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


Check to see if the address is bound to one of your interfaces.

0

精彩评论

暂无评论...
验证码 换一张
取 消