2012-02-12 73 views
0

我试图在Linux上连接指纹设备(tc400)C2设备。硬件供应商提供了一个我不能在Linux上使用的DLL文件,因此我尝试使用套接字编程并使用十六进制代码与设备进行通信。问题是设备根本没有响应,当我尝试获取客户端套接字的输入流时,代码崩溃。 这是我的所有代码:使用Java和Hex连接C2设备

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

如果有人能帮助我与设备我会感激通信。

+0

我可以问问你是怎么做到的?我有C3设备,我只能在Windows上与它进行通信,非常适合使用linux替代品。 – Jeremy 2012-03-20 10:40:33

回答

0

在字符串中存储二进制数据是恕我直言,一个坏主意。特别是因为String的getBytes()方法根据系统默认字符集(un Linux主要是UTF-8)返回字节,这可能会产生意外的字节序列。

您应该更好地使用byte[]而不是String。字节阵列也可以以这种方式定义:

new byte{(byte) 0xA5, (byte) 0x00, (byte) 0x00 (byte) 0x00 (byte) 0x00 (byte) 0x30 (byte) 0x00 (byte) 0x00 (byte) 0x51 (byte) 0x10} 

或者您可以使用Apache common Codec Hex类用于转换十六进制字符串到字节数组。

第二个错误是使用ObjectInputStreamreadObject()来读取重新激活的数据。它专为读取Java序列化类数据而设计。使用更好的使用DataInputStream和验证码:

byte msg [] = new byte[1024]; 
System.out.println("D6"); 
int bytesReturned = in.read(msg, 0, msg.length); 

注意,结果可能是不完整的情况下,该数据不转移作为一个块。如果您知道msg大小的确切响应,请改为使用readFull(msg)。

+0

真的很幸运,看到这个问题也是你的答案,请问我有同样的问题在这里stackoverflow.com/questions/23990924/... :(可以请你帮我!我将不胜感激!@罗伯特 – 2014-06-04 07:52:28