2012-08-12 132 views
-1

我嘗試PLC(電子設備)和PC之間的通信。防火牆關閉。我看到wireshark收到的包裹。C#UDP客戶recive太慢

問題1:接收messagges太慢,爲什麼呢?它需要幾次才能到達我的代碼。我的代碼如下。

問題2:如何是Wireshark軟件迅速佔領這個消息?我怎樣才能在C#中實現這一點?

問題3:我不得不關閉防火牆用於接收消息。但wireshark不需要關閉防火牆。如何通過永不關閉防火牆來實現這一點。我嘗試基本上1對1本地通信。

private void udpcommincate() 
    { 
     sock_rcv = new UdpClient(6002); 
     try 
     { 
      sock_rcv.BeginReceive(new AsyncCallback(recv), null); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.ToString()); 
     } 
    } 
    private void recv(IAsyncResult res) 
    { 
     IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 6002); 
     plc_gelen = sock_rcv.EndReceive(res, ref RemoteIpEndPoint); 
     flag= BitConverter.ToInt32(plc_gelen, 0); 
     sock_rcv.BeginReceive(new AsyncCallback(recv), null); 
    } 

回答

2
  1. 對於你並不需要所有這種異步機械簡單的UDP通信 - 它需要時間來POST請求的線程池,調度回調,等等,等等。如果你想要的速度,只是堵讀在一個循環中,全部在一個線程中。
  2. 和3 Wireshark的水龍頭成得到的所有數據包匹配給定過濾器的副本,經常在內核防火牆之前得到它的手,爲他們特殊的內核接口(在winpcap庫中實現)。