我有一个应用程序,该应用程序连接两个手机使用热点发送/recv文件。一部手机创建热点(Wifi hotspot ),我用下面的代码连接另一部热点手机:
mSocket = new Socket();
try {
mSocket.setReuseAddress(true);
mSocket.bind(null);
mSocket.setPerformancePreferences(0, 1, 2);
mSocket.setTcpNoDelay(true);
mSocket.setSoLinger(true, 0);
mSocket.setKeepAlive(true);
mSocket.connect(new InetSocketAddress(mConnection.getServerAddress(),
mConnection.getServerPort()), SOCKET_CONNECT_TIMEOUT);
//operation here....
} catch (SocketException e) {
((HotSpotConnection)mConnection).getlocalip();
LogUtils.LOGE(LOG_TAG, "init exception:" + e.toString());
} catch (IOException e) {
LogUtils.LOGE(LOG_TAG, "init exception:" + e.toString());
}
我得到了下面的错误:
E/MyTestWifiThread(18918): init exception:java.net.ConnectException:
failed to connect to /192.168.43.1 (port 9876) after 30000ms:
isConnected failed: ENETUNREACH (Network is unreachable)
为什么无法连接热点电话,我确定热点电话地址默认为192.168.43.1?因为我pinged了192.168.43.1,热点电话回应了。
发布于 2015-12-25 10:11:05
我以前遇到过这个问题。这对我很有效。
更改套接字的通信方式。
尝试在连接到热点的客户端上打开服务器套接字,并在系留设备上创建客户端套接字以连接到服务器套接字。
Android connect to tethering socket
这可能会有帮助!
https://stackoverflow.com/questions/27241719
复制相似问题