简体   繁体   中英

How to create a socket that listens a localhost port in Android

Before explain my doubt I want to mension that there're three "actors" in this enviorement:

  • A desktop program executed in some server
  • A person who uses this program remotely through a computer
  • And an Android tablet application that is connected with the computer via USB

When the person clicks on a link bar in this program that he is using remotely there's a shell script that sends a string to the person's computer port(eg 1100). The requirement is: After recive this string from the desktop send it to the tablet.

So, what is the best way to recive this string from the desktop to the tablet?

Now there's a java daemon that is running in the desktop computer who is listening the 1100 port. This java app is working as a intermidiate between the desktop computer and the tablet, but I think that maybe there's a way to delete this daemon and just using the tablet to recive the computer data creating a socket.

You can also implement Java Sockets in Android. Like wise on Desktop Sockets in Android listens on ports but this will be accessed locally as there is no such way to assign static usable IP to an Android device.

There is another solution which is socket.io Socket.io is one of the best socket library i have ever used. Implement socket.io on Node.js server and on Android simply connect to Node.js server and you can send same string to all connected users whether it is a Desktop or Android.

private class ServerThread extends Thread {

    @Override
    public void run() {
        try {
            serverSocket = new ServerSocket(portNumber);

            while (true) {
                try {
                    clientSocket = serverSocket.accept();
                } catch (IOException e) {
                    LOGGER.error("Error on accept!");
                }
                // new thread for a client
                new EchoThread(clientSocket).start();
            }

        }catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

public class EchoThread extends Thread {
    protected Socket clientSocket;
    BufferedReader br = null;

    public EchoThread(Socket clientSocket) {
        this.clientSocket = clientSocket;
    }

    public void run() {
        try {
             br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        } catch (IOException e) {
            ...
        }

        isConnected = true;

        while (isConnected) {
            try {
                receivedMessage = br.readLine();
                if (!Strings.isNullOrEmpty(receivedMessage)) {
                    ....
                }
                else {
                    //sleep 0,1s
                    //TODO (ver): Thread.sleep(100);
                }
            } catch (Exception e) {
                if (e instanceof IOException) {
                }
                else if (e instanceof JsonSyntaxException){
                }
                else {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException inte) {
                        inte.printStackTrace();
                    }
                }
            }

            if (//yourmessagecondition){
                isConnected = false;
            }
        }
        if (!isConnected){
            try {
                br.close();
                clientSocket.close();
            }catch (IOException ioe){
                ioe.printStackTrace();
            }
        }
    }
}

Hope this helps, it is working here... You probably find easier/simple examples on the Internet. PS: If you are using a USB cable to communicate with the computer app, you can use the "adb forward" command, just google for some examples and accept this answer if it helped ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM