2012-10-16 71 views
0

我正在開發一個應用程序以向端口502發送請求並希望從同一端口讀取響應。Android如何讀取端口502上的TCP客戶端的響應

這是我試過到目前爲止

public class Autoamtion extends Activity { 

    Socket echoSocket = null; 
    PrintWriter out = null; 
    BufferedReader in = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
       .permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 
     Log.v("on create", "on create"); 
     final ToggleButton fanOn = (ToggleButton) findViewById(R.id.toggleButton1); 
     fanOn.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       if (fanOn.isChecked()) { 
        Toast.makeText(Autoamtion.this, "Fan is On", 
          Toast.LENGTH_SHORT).show(); 
        Log.v("on create", "on create"); 
        try { 
         echoSocket = new Socket("192.168.1.19", 502); 
         out = new PrintWriter(echoSocket.getOutputStream(), 
           true); 
         in = new BufferedReader(new InputStreamReader(
           echoSocket.getInputStream())); 
        } catch (UnknownHostException e) { 
         System.err.println("Don't know about host: taranis."); 
         System.exit(1); 
        } catch (IOException e) { 
         System.err.println("Couldn't get I/O for " 
           + "the connection to:192.168.1.19"); 
         System.exit(1); 
        } 

        BufferedReader stdIn = new BufferedReader(
          new InputStreamReader(System.in)); 
        String userInput; 

        try { 
         while ((userInput = stdIn.readLine()) != null) { 
          out.println(userInput); 
          System.out.println("echo: " + in.readLine()); 
         } 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        out.close(); 
        try { 
         in.close(); 

        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        try { 
         stdIn.close(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        try { 
         echoSocket.close(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } else 
        Toast.makeText(Autoamtion.this, "Fan is Off", 
          Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 
} 

我剛安裝這將打開端口502我證實了一個simulator端口它獲取運行此simulator後開放。
我剛剛建立了與502端口的連接。我沒有從那裏得到任何迴應。

請引導我通過這個,所以我可以使這個工作。任何意見和幫助,將不勝感激。
謝謝

回答

0

你也應該發佈stackTrace,以檢查什麼是failling。

至少有一件事情在您的信息中不一致,請參考port 503中的文字和您使用的代碼echoSocket = new Socket("192.168.1.19", 502);連接到端口502

+0

感謝您的回答,我剛糾正。我沒有得到任何錯誤,但即使我沒有得到任何反應,該simulator.did我錯過了東西來閱讀迴應? – juned

+0

您的代碼未讀取模擬器的響應。它看起來只是從'System.in'和寫入模擬器。 – Luis

+0

ohhh好吧,我是socket編程的新手,你能給我一些參考,我可以從中學到很多東西。 – juned