2010-11-17 118 views
1

我在與通過WiFi在TCP來自Android客戶端將數據發送到桌面服務器的一些問題。模擬器工作正常,但在實際的電話上,無法建立連接。引發了「套接字未連接」異常。安卓:「套接字未連接」異常

我在下面附上我的代碼。請幫忙嗎?非常感謝!

// CODE的活動中

public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case R.id.send: 
      sendMessage(); 
      return true; 
    /// ...other items 
    } 
} 

private void sendMessage() { 
    String serverAddr = "18.xxx.xx.xxx"; 
    Socket socket = null; 
    try { 
      socket = new Socket(serverAddr, 4444); // EXCEPTION HAPPENS HERE 
    } catch (Exception e) { 
      //show exception on screen 
    } 

    String message = "some message"; 
    try { 
      PrintWriter out = new PrintWriter(socket.getOutputStream(), true); 
      out.println(message); 
    } catch (Exception e) { 
      //show exception on screen 
    } finally { 
      socket.close(); 
    } 
} 
+1

的問題是,你不知道你have..rewrite使用的SocketFactory的代碼是什麼類型的故障爲的SocketFactory給人一種類型的故障,讓你知道你有哪些失敗,而不復測,看看你有什麼類型的故障..它可能是連接超時,或等。 – 2010-11-17 08:58:16

+0

@Fred:謝謝弗雷德。根據您的建議解決問題。 – Crastinator 2011-01-27 04:23:08

回答

1

你serverAddr不應該是一個字符串,但爲的InetAddr。 使用InetAddr.getByName(「18.x.x.x」)

0

服務器地址應在網絡中的服務器的本地地址。 還需要配置路由器「PORT FORWORDING」從4444端口發送的所有數據包,並將其轉發到你的服務器的本地IP。此外,手機和服務器電腦都應連接到同一個WiFi網絡。