我無法通過套接字連接兩個Android手機我正在使用android調試橋將手機連接到我的PC,從中我可以在手機上啓動模擬器。Android Socket連接拒絕
我推出一個手機上的服務器,並嘗試以然而,其他連接我讓我的客戶端以下錯誤:
W/System.err: java.net.ConnectException: failed to connect to /192.168.49.1 (port 8080): connect failed: ECONNREFUSED
服務器
private class SocketServerThread extends Thread {
static final int SocketServerPORT = 8080;
@Override
public void run() {
try {
serverSocket = new ServerSocket(SocketServerPORT);
Log.d("Quiz", "Server creation connection success");
Log.d("Quiz", "Server local port "+serverSocket.getLocalPort());
Log.d("Quiz", "Server local port "+serverSocket.getInetAddress());
HostingScreen.this.runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
while (true) {
Socket socket = serverSocket.accept();
count++;
message += "#" + count + " from " + socket.getInetAddress()
+ ":" + socket.getPort() + "\n";
HostingScreen.this.runOnUiThread(new Runnable() {
@Override
public void run() {
msg.setText(message);
}
});
SocketServerReplyThread socketServerReplyThread = new SocketServerReplyThread(
socket, count);
socketServerReplyThread.run();
}
} catch (IOException e) {
e.printStackTrace();
}
}
客戶
@Override
protected Object doInBackground(Object[] params) {
Socket socket = null;
try {
socket = new Socket(host,port);
Log.d("Quiz", "Client socket success");
connected = true;
while (connected) {
try {
Log.d("Quiz", "Sending command");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true);
// where you issue the commands
} catch (Exception e) {
}
}
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
暫時將端口硬編碼爲服務器和客戶端的端口8080,主機當我提出通過使用下面的代碼手機之間的連接地址被找到:
連接
private void onConnectionChanged(Intent intent) {
final NetworkInfo netInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
final WifiP2pInfo p2pInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_INFO);
isConnected = netInfo.isConnected();
if (isConnected) {
//Get host connection info
wifiMgr.requestConnectionInfo(channel, new ConnectionInfoListener() {
@Override
public void onConnectionInfoAvailable(final WifiP2pInfo info) {
wifiMgr.requestGroupInfo(channel, new GroupInfoListener() {
@Override
public void onGroupInfoAvailable(WifiP2pGroup group) {
if (group == null)
return;
WiFiP2P.this.group = group;
Log.d(TAG, "Wifi p2p connection group is " + group.getNetworkName());
Log.d(TAG, "Group size " + group.getClientList().size());
fireOnConnectionSucceed(group.getNetworkName(), group.getPassphrase());
//create client if not host
if(info.isGroupOwner) {
Client client = new Client(8080, p2pInfo.groupOwnerAddress.getHostAddress());
client.execute();
Log.d(TAG, "Client launch success");
Log.d(TAG, "Host address " + p2pInfo.groupOwnerAddress.getHostAddress());
}
}
});
if (isConnected && !info.isGroupOwner) {
} else {
startDiscovery();
}
}
});
} else {
group = null;
fireOnConnectionLost();
}
}
我檢查,看看是否連接的人是主機或沒有,如果不是我啓動客戶端傳遞客戶端的端口和主機地址,這是創建連接套接字的Aysnc任務。
錯誤時
socket = new Socket(host,port);
使用導致錯誤指出發生在客戶端。
任何想法可能是什麼問題?他們都通過WiFi連接到對方,但是當我嘗試連接到服務器套接字時失敗。
在此先感謝。
編輯: 爲了清除一些東西,我使用adb從我的電腦上獲取應用程序到我的手機上,從中我可以啓動應用程序。
我連接兩臺手機使用wifimanager我需要找到哪些手機創建wifi組,然後那個人是服務器的主機,我可以連接問題出現時,當我嘗試啓動套接字連接使用連接信息中的主機地址發送到主機。
'在手機上啓動模擬器'。 ??????? – greenapps
'我檢查連接的人是不是主人'。對不起,但我不知道你在做什麼。 – greenapps
'我正在使用android調試橋將手機連接到我的電腦'。我沒有理由將它們與您的電腦連接起來。 – greenapps