2016-09-21 66 views
0

我想在c#程序中接收udp包。幾個軟件包組合的內容是一個XML狀態日誌。在一個狀態記錄完成後重複。見下圖。我使用this UDP Broadcast example來獲取包,端口53181(見圖片)。 但該程序沒有收到任何東西。有任何想法嗎?我如何使用收到的包來獲取XML數據?c#接收帶有xml內容的udp廣播包(windows 7)

Wireshark Screen

+0

我的答案解決了你的問題嗎?如果是這樣,請將其標記爲答案。如果不。詳細說明你需要什麼。 –

+0

對不起,但我沒有時間測試你的代碼。謝謝btw。我希望我能在下週進行測試。 – jazz

+0

恩,我對c#編程相當陌生。使用界面的原因是什麼?是否可以通過將命令行打印接收數據的方式完成示例?這將幫助我瞭解如何處理您的代碼。謝謝 – jazz

回答

0

這是一個通用接收器I類製成

public class UdpBroadcastReceiver 
{ 
    private IBroadcastInterpreter _interpreter; 
    private volatile bool _shouldReceive = true; 

    /// <summary> 
    /// Will listen on all ports for udp broadcasts - Will handle input with interpreter 
    /// </summary> 
    /// <param name="interpreter"></param> 
    public UdpBroadcastReceiver(IBroadcastInterpreter interpreter) 
    { 
     _interpreter = interpreter; 
    } 

    /// <summary> 
    /// Will listen for a broadcast indefinetly. Will lock up your program. 
    /// Use Task.Run to start listening. Then you can use StopListening to break the loop. 
    /// </summary> 
    public void ListenForBroadcast(int port) 
    { 
     using (UdpClient client = new UdpClient(port)) 
     { 
      IPEndPoint senderEndPoint = new IPEndPoint(IPAddress.Any, 0); 
      while (_shouldReceive) 
      { 
       byte[] receiveBuffer = client.Receive(ref senderEndPoint); // execution will stop and wait for data. 
       _interpreter?.HandleBroadcast(receiveBuffer); 
      } 
     } 
    } 

    /// <summary> 
    /// Breaks the while loop in ListenForBroadcast 
    /// </summary> 
    public void StopListening() 
    { 
     _shouldReceive = false; 
    } 
} 

public interface IBroadcastInterpreter 
{ 
    void HandleBroadcast(byte[] input); 
} 

的示例實現可以是接口:

class WriteToConsole : IBroadcastInterpreter 
{ 
    public void HandleBroadcast(byte[] input) 
    { 
     string text = Encoding.ASCII.GetString(input); 
     Console.WriteLine(text); 
    } 
} 

這是我製作的UDP廣播器,用於播出時間。

class UdpBroadcaster 
{ 

    private int _port; 
    private bool shouldRun = true; 

    public UdpBroadcaster(int port) 
    { 
     _port = port; 
    } 

    public void Stop() 
    { 
     shouldRun = false; 
    } 

    public void Start() 
    { 
     Trace.TraceInformation("Entered start method"); 
     Task.Run(() => 
       { 
        Trace.TraceInformation("Started task"); 
        IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Broadcast, _port); 

        using (UdpClient client = new UdpClient()) 
        { 
         Trace.TraceInformation("Entered using statement"); 
         client.EnableBroadcast = true; 
         while(shouldRun) 
         { 
          Trace.TraceInformation("Started while loop"); 
          byte[] data = Encoding.ASCII.GetBytes(DateTime.Now.ToString(new CultureInfo("da-dk"))); 
          client.Send(data, data.Length, serverEndPoint); 
          Trace.TraceInformation("waiting 5 seconds"); 
          Thread.Sleep(5 * 1000); 
         } 
        } 
       }); 
    } 

} 

創建一個WriteToConsole類的實例,並將其傳遞到廣播接收器的構造函數中。然後,無論何時收到廣播,它都將使用接口的實現來執行操作。在這種情況下,根據您的請求 - 它只會寫入控制檯。