sockets usb hotspot - Android connect to tethering socket




1 Answers

Server Side

class ServerThread implements Runnable {

    public void run() {
        socket = new Socket();

        try {
            serverSocket = new ServerSocket(SERVERPORT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        while (!Thread.currentThread().isInterrupted()) {
            try {
                    socket = serverSocket.accept();

                CommunicationThread commThread = new CommunicationThread(socket);
                new Thread(commThread).start();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Client Side

class ClientThread implements Runnable {

    @Override
    public void run() {
        try {
            socket = new Socket();
            InetSocketAddress socketAddress = new InetSocketAddress(SERVER_IP, SERVERPORT);
            socket.connect(socketAddress);

            CommunicationThread commThread = new CommunicationThread(socket);
            new Thread(commThread).start();

        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}
wifi how laptop

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?




Related