2012-06-26 23 views
2

我試圖讓我的android與我的電腦談話,我幾乎已經知道了。我可以通過互聯網發送字符串到我的電腦和所有,但只有一個問題:當我的電腦讀取字符串,他們是空的!無聲字符串的祕密(服務器接收來自Android客戶端的空字符串)

這裏的客戶端代碼:

public class ClientActivity extends Activity implements View.OnClickListener{ 

EditText ip1,ip2,ip3,ip4, message, port; 
TextView con; 
Button send; 
InetAddress inet; 
Socket s; 
OutputStream out; 
PrintWriter output; 
int sip; 
int rport; 
byte[] ip; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    ip1 = (EditText) findViewById(R.id.etIP1); 
    ip2 = (EditText) findViewById(R.id.etIP2); 
    ip3 = (EditText) findViewById(R.id.etIP3); 
    ip4 = (EditText) findViewById(R.id.etIP4); 
    port = (EditText) findViewById(R.id.etPort); 
    message = (EditText) findViewById(R.id.etMessage); 
    send = (Button) findViewById(R.id.bSend); 
    con = (TextView) findViewById(R.id.tvCon); 
    ip = new byte[4]; 

    send.setOnClickListener(this); } 

@Override 
public void onClick(View arg0) { 
    switch(arg0.getId()){ 
     case R.id.bSend: 

     try{ 
      rport = Integer.parseInt(port.getText().toString()); 
     } catch (NumberFormatException e){ 
      con.setText(e.toString()); 
      break; 
     } 

     try{ 
      sip = Integer.parseInt(ip4.getText().toString()); 
      sip = (sip << 8) + Integer.parseInt(ip3.getText().toString()); 
      sip = (sip << 8) + Integer.parseInt(ip2.getText().toString()); 
      sip = (sip << 8) + Integer.parseInt(ip1.getText().toString()); 
     } catch (NumberFormatException e){ 
      con.setText(e.toString()); 
      break; 
     } 

     ip[0] = (byte)(0xff & sip); 
     ip[1] = (byte)(0xff & (sip >> 8)); 
     ip[2] = (byte)(0xff & (sip >> 16)); 
     ip[3] = (byte)(0xff & (sip >> 24)); 

     try { 
      inet = InetAddress.getByAddress(ip); 
      s = new Socket(inet, rport); 
      out = s.getOutputStream(); 
      output = new PrintWriter(out); 
      output.println(message.getText().toString()); 
      con.setText("Message Sent"); 
      s.close(); 
     } catch (UnknownHostException e) { 
      con.setText(e.toString()); 
     } catch (IOException e) { 
      con.setText(e.toString()); 
     } 
     break; 
    } 
} 
} 

下面是該服務器的代碼:

public class Main { 
public static void main(String[] args) { 
    try { 
     Boolean end = false; 
     ServerSocket ss = new ServerSocket(4331); 
     while(!end){ 
       //Server is waiting for client here, if needed 
       Socket s = ss.accept(); 
       BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream())); 
       PrintWriter output = new PrintWriter(s.getOutputStream(),true); //Autoflush 
       String st = input.readLine(); 
       System.out.println(""+st); 
       s.close();  
     } 
     ss.close(); 

     } catch (UnknownHostException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } 
    } 
} 

任何幫助都將不勝感激。

編輯:我發現了問題;最初我沒有使用模擬器來測試我的代碼,我使用我的實際Android,因爲我無法讓無線網絡模擬器工作(這是一個簡單的修復),所以當我的模擬器上運行代碼它的工作原理,沒有空字符串;但是當我的真正的Android上運行時,它返回null,只有一個原因,我可以想到會導致這種情況發生,我使用的SDK不匹配我的Android操作系統版本(它的舊版本)可能是這個原因或我錯了嗎?

編輯:我知道了,原來我只需要安裝一些API從機器人SDK管理器10包就像現在一個魅力:)

+0

您是否在wifi上使用您的實際設備或超過3g/4g?許多運營商阻止非標準端口,有時在他們的網絡,有時在設備本身 –

+0

實際的WiFi我買了三星銀河媒體播放器,它沒有電話功能 – MrFuzzles

回答

0

的空字符串只是用於readline以突出的方式在客戶端撥打close後,輸入流已結束。

你的代碼基本上應該工作。在連接之前,通過在客戶端套接字上調用setTcpNoDelay來添加更多日誌記錄並禁用Nagle算法以實現更快(更簡單)的調試。

+0

我試着調用setTcpNoDelay,我仍然在服務器端接收空,這可能發生的任何其他原因? – MrFuzzles

+0

@ user1483936 - 請告訴我們在服務器收到null的客戶端的確切時間。添加日誌和睡眠調用或使用調試器來確定。 –

相關問題