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) at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85) at libcore.io.IoBridge.connectErrno(IoBridge.java:127) at libcore.io.IoBridge.connect(IoBridge.java:112) ... 8 more
The server gives no errors.
I have the following permission set in the android manifest file:
<uses-permission android:name="android.permission.INTERNET" > </uses-permission> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" > </uses-permission> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" > </uses-permission>
Ideally I would need to transfer some data from server to the client.
What am I doing wrong?
You can use Bluetooth to tether your smartphone to your laptop. 3. You can use direct connection via a USB-A cable. For Android phones, this is typically either a USB-A to USB-C or USB-A to Micro USB cable.
By testing it turns out the connection cannot be established on the tethering device.
But if we reverse the process and open a serversocket on the connected client. And connect to it from the tethering device, it will work.
So reversing the communication is the answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With