我无法通过套接字连接两个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