简体   繁体   English

安卓服务器PC客户端通信

[英]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).我正在解决一个问题,我必须将数据从 PC 应用程序(用 java 编写)发送到 android 应用程序(java)。 It is a cash register application that need to display bill details on android app.这是一个收银应用程序,需要在Android应用程序上显示帐单详细信息。 While there is no bill, android app need to display something else (pictures etc.) Cash register application already exists, it is desktop PC software.虽然没有账单,android app 需要显示其他东西(图片等) 收银机应用程序已经存在,它是桌面 PC 软件。

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.我开始使用套接字,其中 android 应用程序是一个服务器,等待 PC 上的收银机应用程序开始连接。 When this happen, connection is open and cash register is sending JSON Strings until the end of a bill.发生这种情况时,连接打开并且收银机正在发送 JSON 字符串,直到账单结束。

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.我选择 android 作为服务器,因为一个收银机可能连接了多个 android,因此它可以在多个“屏幕”上显示帐单详细信息,并使 android 应用程序保持特定端口始终打开并侦听成为可能它为客户。

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.我还阅读了有关 RMI java 的内容,但不知道我是否应该那样做。 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.我制作了PC服务器和android客户端。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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