31
I have 2 android devices, one acts as a server that is in tethering mode, and accepting connections to a port. The other device acts as a client that connects to the server hotspot and establishes a connection to the server via a port. (Everything is done without a router in the middle).
The server code looks like this: (When the android server executes the code, the device is in tethering mode)
ServerSocket server = new ServerSocket(PORT);
while (true) {
Socket client;
client = server.accept();
ClientThread com = new ClientThread(client, this);
Thread t = new Thread(com);
t.start();
}
The client code looks like this: (when the android client executes this code, the device is connected to access point)
Socket client = new Socket();
int serverIP = wifiManager.getDhcpInfo().serverAddress;
String stringIP = android.text.format.Formatter.formatIpAddress(serverIP);
InetAddress address = InetAddress.getByName(stringIP);
SocketAddress socketAddress = new InetSocketAddress(address, PORT);
client.connect(socketAddress);
I get the following error on the client
java.net.ConnectException: failed to connect to /192.168.43.1 (port 12345): connect failed: ENETUNREACH (Network is unreachable)
at libcore.io.IoBridge.connect(IoBridge.java:114)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
at java.net.Socket.connect(Socket.java:842)
at java.net.Socket.connect(Socket.java:785)
at com.alternatewifimeshmessaging.HostManager.clientConnect(HostManager.java:283)
at com.alternatewifimeshmessaging.HostManager.client(HostManager.java:189)
at com.alternatewifimeshmessaging.HostManager.run(HostManager.java:59)
at java.lang.Thread.run(Thread.java:856)
Caused by: libcore.io.ErrnoException: connect failed: ENETUNREACH (Network is unreachable)
at libcore.io.Posix.connect(Native Method)
a