Skip to content Skip to sidebar Skip to footer

Android Connect To Tethering Socket

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

Solution 1:

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.

Solution 2:

Server Side

classServerThreadimplementsRunnable {

    publicvoidrun() {
        socket = newSocket();

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

                CommunicationThreadcommThread=newCommunicationThread(socket);
                newThread(commThread).start();

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

Client Side

classClientThreadimplementsRunnable {

    @Overridepublicvoidrun() {
        try {
            socket = newSocket();
            InetSocketAddresssocketAddress=newInetSocketAddress(SERVER_IP, SERVERPORT);
            socket.connect(socketAddress);

            CommunicationThreadcommThread=newCommunicationThread(socket);
            newThread(commThread).start();

        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

Solution 3:

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.

Post a Comment for "Android Connect To Tethering Socket"