2010-09-20 149 views
0

我目前正在DatagramSocket上發送一個DatagramPacket,我收到了很好..問題是我正在接收我發出的數據包。如果我打電話給接收兩次然後超時。是否有辦法忽略第一個數據包並接收第二個數據包。Android廣播UDP循環

這裏是我的代碼..

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

回答