2013-04-30 90 views
2

我用Android設備Android和PC Socket連接

/* 
* This is a simple Android mobile client 
* This application read any string message typed on the text field and 
* send it to the server when the Send button is pressed 

*/ 
package lakj.comspace.simpleclient; 

import java.io.IOException; 
import java.io.PrintWriter; 
import java.net.Socket; 
import java.net.UnknownHostException; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

public class SimpleClientActivity extends Activity { 

    private Socket client; 
    private PrintWriter printwriter; 
    private EditText textField; 
    private Button button; 
    private String messsage; 

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

     textField = (EditText) findViewById(R.id.editText1); //reference to the text field 
     button = (Button) findViewById(R.id.button1); //reference to the send button 

     // Button press event listener 
     button.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View v) { 

     messsage = textField.getText().toString(); //get the text message on the text field 
     textField.setText("");  //Reset the text field to blank 

     try { 

      client = new Socket("10.0.2.2", 4444); //connect to server 
      printwriter = new PrintWriter(client.getOutputStream(),true); 
      printwriter.write(messsage); //write the message to output stream 

      printwriter.flush(); 
      printwriter.close(); 
      client.close(); //closing the connection 

     } catch (UnknownHostException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
}); 

} 
} 

以下代碼作爲客戶端並遵循服務器端的簡單的Java項目

/* 
* This is a simple server application 
* This server receive a string message from the Android mobile phone 
* and show it on the console. 
* Author by Lak J Comspace 
*/ 
package simpleserver; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.ServerSocket; 
import java.net.Socket; 

public class Main { 

private static ServerSocket serverSocket; 
private static Socket clientSocket; 
private static InputStreamReader inputStreamReader; 
private static BufferedReader bufferedReader; 
private static String message; 

public static void main(String[] args) { 

    try { 
     serverSocket = new ServerSocket(4444); //Server socket 

    } catch (IOException e) { 
     System.out.println("Could not listen on port: 4444"); 
    } 

    System.out.println("Server started. Listening to the port 4444"); 

    while (true) { 
     try { 

      clientSocket = serverSocket.accept(); //accept the client connection 
      inputStreamReader = new InputStreamReader(clientSocket.getInputStream()); 
      bufferedReader = new BufferedReader(inputStreamReader); //get client msg      
      message = bufferedReader.readLine(); 

      System.out.println(message); 
      inputStreamReader.close(); 
      clientSocket.close(); 

     } catch (IOException ex) { 
      System.out.println("Problem in message reading"); 
     } 
    } 

    } 
} 

我使用了簡單的按鈕發送字符串從Android模擬器到Java應用程序,但它給連接error.Which端口和IP我應該使用,而不是代碼中提到的...以及如何得到,請幫助我

,我如何修改此代碼發送從android到PC的移動聯繫人?

+1

聯繫開發機器的特殊10.0.2.2地址只適用於模擬器,不適用於物理的android設備。此外,這個問題應該是封閉的,因爲它與衆多其他問題相同。 – 2013-04-30 18:21:31

+0

它給*什麼*連接錯誤? – EJP 2017-09-24 04:24:16

回答

0

您的電腦的主機地址可能有誤,如果您正在運行Windows計算機轉到您的開始菜單並在搜索框中輸入「cmd」,您應該看到一個黑匣子彈出框,輸入「ipconfig」

enter image description here

所以,如果我還是被棟該應用我會使用IP地址10.0.0.129。使用從9152到65535的任何端口。您可能希望將IP地址設置爲靜態,以便在測試應用時不會改變您的位置。按照本教程爲您提供幫助http://www.howtogeek.com/howto/19249/how-to-assign-a-static-ip-address-in-xp-vista-or-windows-7/這將允許您在本地網絡上測試您的應用程序,而無需更改計算機IP地址。

如果您想在本地網絡之外使用此應用程序,您需要租用專用服務器,安裝Java Web服務器或將您的機器用作服務器。要使用您的機器,您需要一個靜態IP地址或DNS服務,我使用http://dyn.com/dns/爲我的計算機分配一個主機名,以便隨時隨地使用我的電腦(只要它打開)。另請注意,如果您選擇使用計算機,則需要在路由器上設置端口轉發。只需查看端口轉發,你會發現大量的教程。

祝你好運。

+0

它不工作 – 2013-04-30 17:45:07

+0

這應該是「本地網絡」,而不是「本地主機」 – 2013-04-30 18:20:20

+0

好抓克里斯,感謝澄清 – 2013-04-30 18:51:04

0

連接你的設備與同一網絡,然後它應該工作。
簡單的方法是:啓用設備的熱點並在此(熱點)網絡上連接您的PC。
不要忘記更改IP。