我目前正在舉辦我的Android手機上的服務器插座之間套接字連接,並使用我的電腦連接到一個。有幾個應用程序已經這樣做(例如Skype),所以它必須是可能的。 我目前面臨的問題是,我無法連接到在我的模擬器上打開的服務器套接字。 這是我在Android的開發工具模擬器中運行,打開我的服務器套接字代碼:無法建立的Java和Android
public void run(){
Log.i(TAG, "1");
try{
serverSocket = new ServerSocket(25555);
Log.i(TAG, "2. Details: " + serverSocket);
while(true)
{
clientSocket = serverSocket.accept(); //it keeps sticking here, and waiting for connections.
Log.i(TAG, "3");
in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
out = new PrintWriter(clientSocket.getOutputStream(), true);
String input = in.readLine();
out.println("received: " + input);
in.close();
out.close();
}
} catch(Exception e) {
Log.i(TAG, "ERROR:");
e.printStackTrace();
}
}
,這是我在正常的Eclipse運行的代碼連接到該服務器套接字:
try {
socket = new Socket(-snip-,25555);
System.out.println("Connection has been made");
} catch (NumberFormatException e) {
System.out.println("Connection wasn't made. Closing the application.");
}
這只是給了我這個錯誤:
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
現在的問題是,有什麼事情我做錯了我的代碼,或者是它的一些問題與模擬器?
順便說一句,我說的這些代碼行到我的AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
在此先感謝。
新的Socket(-snip-,25555);什麼是-snip-? –
我把自己的IP切掉了。不喜歡只站在網上:P。 – user3254705
是'-snip-'你的電腦IP或模擬器IP? –