【发布时间】:2014-07-22 08:35:20
【问题描述】:
我正在尝试使用 Java 程序连接生物识别指纹考勤设备。我使用的设备是 Biocom 指纹考勤系统。但是,我正在搜索和阅读相关内容,我看到可以使用基于设备类型的 SDK(这很难,不合逻辑,而且,它不是全局解决方案!)
我研究了有关如何使用指纹设备连接、发送和检索数据的全球标准,但我还是没有幸运地找到明确的解决方案。目前,我尝试通过创建Socket 对象(通过以太网端口)与设备连接,但也没有与我一起执行。这个打开的无限循环问题在我头上。
- 是否有任何通用的标准方法可以使用 Java 从此类设备连接、发送和检索数据?
- 可以将
Socket视为此类问题的解决方案吗? - 如果是,我下面的代码有什么问题?除了主机 IP 和端口号之外,还需要什么其他东西来连接设备?
使用的 Socket 代码:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Requester {
Socket requestSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Requester() {
}
void run() throws IOException {
try {
// 1. creating a socket to connect to the server
requestSocket = new Socket("192.168.0.19", 4370);
System.out.println("Connected to given host in port 4370");
// 2. get Input and Output streams
in = new ObjectInputStream(requestSocket.getInputStream());
// 3: Communicating with the server
String line;
while (true) {
line = in.readLine();
if (line != null) {
System.out.println(line);
}
}
} catch (UnknownHostException unknownHost) {
System.err.println("You are trying to connect to an unknown host!");
} catch (IOException ioException) {
ioException.printStackTrace();
} catch (Exception Exception) {
Exception.printStackTrace();
} finally {
in.close();
requestSocket.close();
}
}
void sendMessage(String msg) {
try {
out.writeObject(msg);
out.flush();
System.out.println("client: " + msg);
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
public static void main(String args[]) throws IOException {
Requester client = new Requester();
client.run();
}
}
这张图片可能会提供更多细节:
【问题讨论】:
-
那么...应用程序表现如何?它会抛出一些异常吗?是不是什么都没读?
-
是的,它没有读取任何内容!它在这条线之后停止 in = new ObjectInputStream(requestSocket.getInputStream()); !!这意味着它没有到达设备(可能)!
-
ObjectInputStream 反序列化原始数据和以前使用 ObjectOutputStream 编写的对象。您意识到您尝试接收的数据没有被 ObjectOutputStream 序列化,因此不会接收到正确的输入,请尝试将其包装在更通用的 InputStream 中,例如简单的 BufferedReader: BufferedReader reader = new BufferedReader(new InputStreamReader( requestSocket.getInputStream()))
-
谢谢,但我真的试过了,但没有希望:((!)我不知道是什么问题而且我一直卡在没有任何进展的情况下!!没有任何教程,真是无限郁闷(而且没有人可以问:()!@TFC
标签: java sockets network-programming fingerprint biometrics