Connect phone error wifi hotspot: ENETUNREACH
I have an app, the app connects two phones using hotspot to send / recv file. One phone creates a hotspot (Wifi hotspot), I connect another phone with a hotspot using the code below:
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());
}
and I got the following error:
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)
Why can't I connect to the hotspot phone, I'm sure the default hotspot phone address is 192.168.43.1? because I pinged 192.168.43.1 and the hotspot phone answered.
+3
source to share
1 answer
I've encountered this problem before. and it works for me.
Change the communication method for sockets.
Try to open a server socket on the client that connects to the access point and create a client socket on the device to connect to the server.
Android will connect to socket socket
this can help!
0
source to share