android usb - Android connect to tethering socket

2 Answers

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.

bluetooth mobile

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);

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);

I get the following error on the client failed to connect to / (port 12345): connect failed: ENETUNREACH (Network is unreachable)
  at com.alternatewifimeshmessaging.HostManager.clientConnect(
  at com.alternatewifimeshmessaging.HostManager.client(
Caused by: connect failed: ENETUNREACH (Network is unreachable)
  at Method)
... 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 android:name="android.permission.CHANGE_WIFI_STATE" >
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >

Ideally I would need to transfer some data from server to the client.

What am I doing wrong?

In a tethered wifi connection, connection provider can not work as a client. So we need to implement a bidirectional tcp socket connection. Use a server socket with a port number in device which is in tethering mode. This device act as a server. In client device use socket to access the client port with IP address.


android sockets