简体   繁体   中英

Android server PC client communication

I am working on a problem where i have to send data from PC application (written in java) to android application (java). It is a cash register application that need to display bill details on android app. While there is no bill, android app need to display something else (pictures etc.) Cash register application already exists, it is desktop PC software.

What is the best way to do this? It is currently done with writing and reading from a file, but i would like to do it in a better way. I start to work with sockets, where android app is a servers waiting for cash register application on PC to start connection. When this happen, connection is open and cash register is sending JSON Strings until the end of a bill.

I chose android to be server because of the possibility that one cash register have more than one android connected so it can display bill details on more than one "screen", and also to make possible that android app keep specific port always open and listen on it for client.

Is this a good way to do it? I just read about possibility that socket connection may die during the non-use period and that could be hardware issue. I read also about RMI java and don't know if i should go that way. I have never worked on communication between devices so i appreciate every suggestion.

I did as suggested and changed logic. I made PC server, and android client.

This is the code for test server app if anyone needs it. It is simple server that send messages entered in terminal to client over chosen port.

public static void main(String[] args) {

    while (true) {
        try {
            ServerSocket serverSocket = new ServerSocket(12345);
            Socket socket = serverSocket.accept();

            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());

            while (true) {
                if (socket.isConnected()) {
                    System.out.println("connected");
                }
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

                // Reading data using readLine
                String name = bufferedReader.readLine();
                System.out.println(name);

                dataOutputStream.writeUTF(name);
                if (false) break;
            }

            socket.close();
            serverSocket.close();
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

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