2016-08-22 94 views
0

我正在嘗試爲這個「異步」嵌入式卡的LED標誌寫一個網絡接口。現有的軟件名爲「PlutoManager」,但它是在中國生產的,對於我們的老客戶來說太難了。從wireshark數據包自定義TCP頭/複製TCP頭

該軟件通過與以太網電纜與嵌入式卡(稱爲PSD100)進行交互來完成許多操作。

我看了一些文檔,文檔指出卡通過標準的TCP/IP協議進行通信。 (或類似的TCP/IP,搞不清東西)

我翻譯了一些東西從中國的文件我得到阿霍德的,這就是我來了解該卡的協議:

(我不太瞭解TCP/IP,所以這種翻譯可能很粗糙,請記住這些詞可能是錯誤的詞,這可能是我的問題的重要組成部分。)

因此,對於每個與卡(發送文件,握手,改變LED標誌的亮度等)有兩件事情必須發生:

  • 的消息被髮送給卡(請求分組)
  • 從卡接收到的應答(應答包)

請求分組結構如下:(來自中國,和我的翻譯很爛)

> 1. Header: 1 byte (16 bits) with a hex value of "0x02" 

>2. Card Address(??): 2 bytes (32 bits) 
>3. Packet Type: 2 bytes (32 bits) 
>4. data: indeterminate length 
>5. CRC Check: 2 bytes (32 bits) 
>6. End of Text Character: 1 byte (16 bits) (value: "0x03" [I guess that's equal to ^c ?] 

這是否看起來像正常的TCP/IP結構,不然我和定製包運走之前?

我想我可以使用Wireshark來嗅探PlutoManager握手時發送的數據包。我還在C#中編寫了一些代碼,試圖與設備的端口建立連接。這是兩個並排的。請注意,這只是轉儲的TCP數據包部分,wireshark輸出的TCP部分是唯一不同的部分。

TCP SEGMENT CAPTURED FROM WIRESHARK HEX + ASCII DUMP (FROM MY C# CODE) 
HEX 
0000 d0 6b 7a 43 5e a3 79 62 67 78 dc bf 50 10 80 51  ASCII: .kzC^.ybgx..P..Q 
0010 46 60 00 00            F`.. 

TCP SEGMENT CAPTURED FROM WIRESHARK HEX + ASCII DUMP (PLUTOMANAGER CODE) 

HEX 
0000 7a 42 d0 6a 34 17 04 36 5e a3 0b 1d 50 10 01 00  ASCII: zB.j4..6^...P... 
0010 82 50 00 00  

我算了一下,「哎,我可以只發送自定義負載到卡用的send()命令和複製什麼PlutoManager代碼做什麼!」

我不知道這個中文軟件是否使用一些特殊的TCP有效載荷來發送消息到標誌,或者它是否使用標準協議。我不知道如何發現差異。我曾嘗試使用Pcap.net發送自定義有效內容,但在繼續沿着拉比方向走下去之前,似乎有必要這樣做? 第二個Wireshark輸出是TCP/IP協議中常見的東西嗎?是否可以發送字符串「zB/^ T3mPP」(這是握手的十六進制轉儲輸出)以便進行握手?

這是如何我現在有我的節目結構(基本上STR:

using System; 
using System.Net; 
using System.Net.Sockets; 
using System.Threading; 
using System.Text; 

// State object for receiving data from remote device. 
public class StateObject 
{ 
    // Client socket. 
    public Socket workSocket = null; 
    // Size of receive buffer. 
    public const int BufferSize = 256; 
    // Receive buffer. 
    public byte[] buffer = new byte[BufferSize]; 
    // Received data string. 
    public StringBuilder sb = new StringBuilder(); 
} 

public class AsynchronousClient 
{ 
    // The port number for the remote device. 
    private const int port = 31298; 

    // ManualResetEvent instances signal completion. 
    private static ManualResetEvent connectDone = 
     new ManualResetEvent(false); 
    private static ManualResetEvent sendDone = 
     new ManualResetEvent(false); 
    private static ManualResetEvent receiveDone = 
     new ManualResetEvent(false); 

    // The response from the remote device. 
    private static String response = String.Empty; 

    private static void StartClient() 
    { 
     // Connect to a remote device. 
     try 
     { 
      // Establish the remote endpoint for the socket. 
      // The name of the 
      // remote device is "host.contoso.com". 
      //IPHostEntry ipHostInfo = Dns.Resolve("host.contoso.com"); 
      IPAddress ipAddress = IPAddress.Parse("192.168.0.59"); //ipHostInfo.AddressList[0]; 
      IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); 

      // Create a TCP/IP socket. 
      Socket client = new Socket(AddressFamily.InterNetwork, 
       SocketType.Stream, ProtocolType.Tcp); 

      // Connect to the remote endpoint. 
      client.BeginConnect(remoteEP, 
       new AsyncCallback(ConnectCallback), client); 
      connectDone.WaitOne(); 

      // Send test data to the remote device. 
      Send(client, "This is a test<EOF>"); 
      sendDone.WaitOne(); 

      // Receive the response from the remote device. 
      Receive(client); 
      receiveDone.WaitOne(); 

      // Write the response to the console. 
      Console.WriteLine("Response received : {0}", response); 

      // Release the socket. 
      client.Shutdown(SocketShutdown.Both); 
      client.Close(); 

     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 
    } 

    private static void ConnectCallback(IAsyncResult ar) 
    { 
     try 
     { 
      // Retrieve the socket from the state object. 
      Socket client = (Socket)ar.AsyncState; 

      // Complete the connection. 
      client.EndConnect(ar); 

      Console.WriteLine("Socket connected to {0}", 
       client.RemoteEndPoint.ToString()); 

      // Signal that the connection has been made. 
      connectDone.Set(); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 
    } 

    private static void Receive(Socket client) 
    { 
     try 
     { 
      // Create the state object. 
      StateObject state = new StateObject(); 
      state.workSocket = client; 

      // Begin receiving the data from the remote device. 
      client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, 
       new AsyncCallback(ReceiveCallback), state); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 
    } 

    private static void ReceiveCallback(IAsyncResult ar) 
    { 
     try 
     { 
      // Retrieve the state object and the client socket 
      // from the asynchronous state object. 
      StateObject state = (StateObject)ar.AsyncState; 
      Socket client = state.workSocket; 

      // Read data from the remote device. 
      int bytesRead = client.EndReceive(ar); 

      if (bytesRead > 0) 
      { 
       // There might be more data, so store the data received so far. 
       state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead)); 

       // Get the rest of the data. 
       client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, 
        new AsyncCallback(ReceiveCallback), state); 
      } 
      else 
      { 
       // All the data has arrived; put it in response. 
       if (state.sb.Length > 1) 
       { 
        response = state.sb.ToString(); 
       } 
       // Signal that all bytes have been received. 
       receiveDone.Set(); 
      } 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 
    } 

    private static void Send(Socket client, String data) 
    { 
     // Convert the string data to byte data using ASCII encoding. 
     byte[] byteData = Encoding.ASCII.GetBytes(data); 

     // Begin sending the data to the remote device. 
     client.BeginSend(byteData, 0, byteData.Length, 0, 
      new AsyncCallback(SendCallback), client); 
    } 

    private static void SendCallback(IAsyncResult ar) 
    { 
     try 
     { 
      // Retrieve the socket from the state object. 
      Socket client = (Socket)ar.AsyncState; 

      // Complete sending the data to the remote device. 
      int bytesSent = client.EndSend(ar); 
      Console.WriteLine("Sent {0} bytes to server.", bytesSent); 

      // Signal that all bytes have been sent. 
      sendDone.Set(); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 
    } 

    public static int Main(String[] args) 
    { 
     StartClient(); 
     return 0; 
    } 
} 

main()的運行命令StartClient(),其嘗試連接,但最終輸出錯誤消息:

System.Net.Sockets.SocketException (0x80004005): No connection could be made because the target machine actively refused it 192.168.0.59:31298 
    at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult) 
    at AsynchronousClient.ConnectCallback(IAsyncResult ar) in C:\Users\xxxxx\Desktop\SocketListenerTest\SocketListenerTest\SocketListenerTest\Program.cs:line 87 

87號線: client.EndConnect(ar);

這讓我覺得,我連接到正確的IP和正確的端口,但內置於.NET中的協議和此嵌入式設備使用的協議不同。

我有權訪問一箇中文文檔(我會發布它,但它是在NDA之下)的一些規格的設備。如果我錯過了一些東西,或者如果您需要更多信息,我會盡我所能。我試圖提供我能夠提供的最相關的信息,但這對我來說很陌生。

我想我可以簡化問題到「如何修改Sockets.Connect()方法以使用自定義TCP協議?」但我認爲最好是對我想要完成的事情給出一個更全面的概述,因爲那可能不是我所需要做的。

感謝您抽出時間看一下這個問題。如果您有任何建議,甚至可以將我指向圖書館或書籍或某種閱讀材料,我很樂意聽到它。謝謝。

+1

你確定它是正確的IP和端口嗎?沒有「自定義TCP協議」。您的規格顯示的是數據包的**有效負載**。由於TCP是基於流的,因此使用wireshark進行嗅探比較困難(我更像是一個UDP人)。 TCP數據包被分段並在到達時按順序放回。然而網絡實施已經這樣做了。 – jython234

回答

0

這個答案可能會晚一點,但是您所得到的SocketException指的是端點拒絕整個連接而不僅僅是自定義部分(位於有效內容中)的事實。

可能是由於所提到的PlutoManager使用特定的源端口進行通信,而嵌入式設備的防火牆拒絕來自所有其他源端口的連接。您可以檢查使用Wireshark的源端口和定義源端口爲您Socket客戶是這樣的:

Socket client = new Socket(AddressFamily.InterNetwork, 
          SocketType.Stream, 
          ProtocolType.Tcp); 
client.Bind(new IPEndPoint(IPAddress.Any, 32000)); //The port number that PlutoManager uses 

我希望這有助於。