2012-02-12 40 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