英文:
Android server PC client communication
问题
我正在处理一个问题,需要从PC应用程序(用Java编写)将数据发送到Android应用程序(Java编写)。这是一个收银机应用程序,需要在Android应用程序上显示账单详情。当没有账单时,Android应用程序需要显示其他内容(图片等)。收银机应用程序已经存在,它是桌面PC软件。
最佳方法是什么?
目前是通过读写文件来实现的,但我想以更好的方式来处理它。
我开始使用套接字(sockets)来工作,其中Android应用程序作为服务器等待PC上的收银机应用程序启动连接。当发生这种情况时,连接打开,收银机会发送JSON字符串直到账单结束。
我选择Android作为服务器,因为一个收银机可能连接多个Android设备,以便在多个“屏幕”上显示账单详情,并且使Android应用程序始终保持特定端口开放并侦听客户端成为可能。
这样做是一个好方法吗?我刚刚了解到套接字连接在非使用期间可能会断开,这可能是硬件问题。我还了解了Java的RMI,不知道是否应该采用这种方式。我从未在设备之间进行通信方面有过经验,所以我很感谢每一些建议。
英文:
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.
答案1
得分: 1
我按建议更改了逻辑。我创建了一个PC服务器和一个Android客户端。
以下是测试服务器应用程序的代码,如果有人需要的话。它是一个简单的服务器,将在终端中输入的消息发送到选择的端口上。
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("已连接");
}
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
// 使用 readLine 读取数据
String name = bufferedReader.readLine();
System.out.println(name);
dataOutputStream.writeUTF(name);
if (false) break;
}
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
希望这对您有帮助。
英文:
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();
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论