2012-05-15 7 views
0

나는 안드로이드 타블렛에서 UDP 클라이언트를 만들려고합니다. 이 응용 프로그램은 소켓 juste 벌금을 만들 수 있습니다,하지만 난 때를 보낼 :안드로이드 플랫폼에서 UDP 패킷을 보냅니다.

android.os.NetworkOnMainThreadException 
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099) 
at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:175) 
at ... 
:

public void SendMessage(String message) 
    { 
     sendBuffer = message.getBytes(); 

     packet = new DatagramPacket(sendBuffer, sendBuffer.length, address, 4445); 
     //packet = new DatagramPacket(sendBuffer, sendBuffer.length, address, port); 

     try 
     { 
      socket.send(packet); 
     } 
     catch (IOException ioe) 
     { 
      Log.d("NETWORK", "Failed to send UDP packet due to IOException: " + ioe.getMessage()); 
      ioe.printStackTrace(); 
     } 
     catch(Exception e) 
     { 
      Log.d("NETWORK", "Failed to send UDP packet due to Exeption: " + e.getMessage()); 
      e.printStackTrace(); 
     } 
    } 

이클립스는 "소스를 찾을 수 없습니다"내가 이것을 로그 캣에서 인쇄 한 말 새 창을 팝업

아마도 내가 사용하고있는 포트가 차단되었거나 뭔가 다른 UDP 포트를 차단할 수 있는지 궁금합니다. 동일한 결과로 여러 포트를 모두 시도했기 때문입니다. 나는 그것 때문에 일부 입력/출력

이 실제 초기화입니다 막고 나타낼 수있는 로그 캣 (BlockGuardOS)의이 부탁 해요 여기에 없습니다 :

public ClientSocketThread(String address, int port) 
    {  
     this.port = port; 

     try 
     { 
      this.address = InetAddress.getByName(address); 
      Log.d("NETWORK", "Address successfully resolved"); 
     } 
     catch(UnknownHostException ue) 
     { 
      ue.printStackTrace(); 
      Log.d("NETWORK", "Failed to resolve ip address due to UnknownException: " + ue.getMessage()); 
     } 

     try 
     { 
      this.socket = new DatagramSocket(); 
      Log.d("NETWORK", "UDP Socket successfully created"); 
     } 
     catch(SocketException se) 
     { 
      Log.d("NETWORK", "Failed to create socket due to SocketException: " + se.getMessage()); 
      se.printStackTrace(); 
     } 
    } 
+0

모바일 네트워크를 통해 보내려고하십니까? 대부분의 모바일 네트워크는 UDP 패킷 (AFAIK)을 차단합니다. –

+0

아니요 라우터를 통해 WiFi에 연결되었습니다 – nevero

답변

6
android.os.NetworkOnMainThreadException 

더 이상 할 수 있습니다 기본 (UI) 스레드에서 네트워킹. 당신은 항상 문서에서 경고를 받았다. 그러나 이제는 적극적으로 갇혀있다.

주제가 여기 및 다른 출처에서 여러 번 다루어졌습니다.

+1

이 스레드를 찾을 미래의 사람들을 위해 [this] (http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception)는 AsyncTask를 올바르게 구현하여 기본 UI 스레드에서 처리하지 않도록하십시오. – Wyatt

관련 문제