I’m trying to connect a fingerprint device (tc400) C2 device on Linux. The hardware vendor provide me with a DLL file which I can’t use on Linux so I tried to use socket programming and communicate with the device using hex code. The problem is the device is not responding at all and the code crashes when I try to get the input stream of the client socket.
This is all of my code:
import java.io.*;
import java.net.*;
public class Client{
Socket requestSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Client(){}
void run()
{
try{
//1. creating a socket to connect to the server
requestSocket = new Socket("172.16.16.192", 5010);
//requestSocket = new Socket("localhost", 5010);
System.out.println("Connected to Machine in port 5010");
//2. get Input and Output streams
System.out.println("-1");
out = new ObjectOutputStream(requestSocket.getOutputStream());
//out.flush();
System.out.println("D0");
sendMessage("A5\\x00\\x00\\x00\\x00\\x30\\x00\\x00\\x51\\x10"); // getting the device information
in = new ObjectInputStream(requestSocket.getInputStream());
//3: Communicating with the server
System.out.println("D1");
do{
System.out.println("D2");
try{
System.out.println("D3");
sendMessage("A5\\x00\\x00\\x00\\x00\\x30\\x00\\x00\\x51\\x10"); // getting the device information
System.out.println("D5");
byte msg [] = null;
System.out.println("D6");
msg = (byte[])in.readObject();
System.out.println("D7");
System.out.println("Machine>" + msg);
//sendMessage(message);
//message = (String)in.readObject();
}
catch(ClassNotFoundException classNot){
System.err.println("data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(UnknownHostException unknownHost){
System.err.println("You are trying to connect to an unknown host!");
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
out.close();
requestSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
void sendMessage(String msg)
{
System.out.println("D4");
try{
System.out.println("D41");
out.writeObject(msg.getBytes());
out.flush();
System.out.println("client>" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void main(String args[])
{
Client client = new Client();
client.run();
}
}
If anyone could help me to communicate with the device I will be grateful.
Storing binary data in Strings is IMHO a bad idea. Especially because the used getBytes() method of String returns bytes depending of the system default charset (un Linux mostly UTF-8) which may produce unexpected bytes sequences.
You should better use
byte[]instead of String. Byte arrays can also be defined this way:Alternatively you can use Apache common Codec Hex class for converting hexadecimal strings to byte array.
The second mistake is the usage of
ObjectInputStreamandreadObject()for reading the redurned data. It is designed for reading Java serialized class data. Use better useDataInputStreamand this code:Note that the result may be incomplete in case the data is not transfered as one block. If you know the exact response msg size use readFull(msg) instead.