我无法通过套接字连接两个Android手机我使用Android调试桥将手机连接到我的电脑,我可以从手机上启动模拟器。
我在一部手机上启动服务器并尝试与另一部手机连接但是我在客户端遇到以下错误:
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组然后那个人是我可以连接的服务器的主机当我尝试启动套接字连接时出现问题主机使用连接信息中的主机地址。
答案 0 :(得分:0)
我设法解决了我的问题,事实证明托管服务器的设备没有被指定为组所有者。
config.groupOwnerIntent=0;
将设备的所有者意图设置为0可解决问题。