2017-05-30 69 views
-1

我有一個產品直接插入到我的計算機的以太網端口,發送多播數據包到224.224.10.10和UDP端口24588.我已經在下面的代碼中設置了我的UDPclient,感覺我已經設置正確,但我仍然沒有收到任何數據包。我總是發現一個例外,說我超時等待迴應。任何想法或明顯的錯誤?使用UDPClient獲取多播消息

在發佈此消息之前,我查看了很多問題,但無法獲得解決方案,而且找不到與我擁有的設置類型相同的任何人。

public class ReceiverClass 
{ 
    private UdpClient m_UDPClient = null; 
    private Thread m_UDPReceiverThread = null; 
    private bool m_ContinueReceiving = false; 
    private readonly object m_sync = new object(); 
    private const int UDP_PORT = 24588; 

    public ReceiverClass() 
    { 
     m_ContinueReceiving = true; 
     m_UDPClient = new UdpClient(UDP_PORT); 
     m_UDPClient.Client.ReceiveTimeout = 20000; 

     m_UDPReceiverThread = new Thread(ReceiveData) { IsBackground = true }; 
     m_UDPReceiverThread.Start(); 
    } 

    private void ReceiveData() 
    { 
     bool Continue; 
     byte[] ReceiveBuffer; 
     IPEndPoint defaultIP = new IPEndPoint(IPAddress.Any, 0); 

     m_UDPClient.JoinMulticastGroup(IPAddress.Parse("224.224.10.10")); 
     m_UDPClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); 

     lock (m_sync) 
     { 
      Continue = m_ContinueReceiving; 
     } 

     while (Continue) 
     { 
      try 
      { 
       ReceiveBuffer = m_UDPClient.Receive(ref defaultIP); 

       if (null != ReceiveBuffer) 
       { 
        // Do stuff with received... 
       } 
      } 
      catch (Exception e) 
      { 
       // ooo eee kill stream 
       Dispose(false); 
       return; 
      } 
      finally 
      { 
       lock (m_sync) 
       { 
        Continue = m_ContinueReceiving; 
       } 
      } 
     } 
    } 
} 

回答

0

我最終什麼事做打算的套接字類和看原始數據包,並挑選出我所需要的。我將套接字綁定到我的lan接口,並嗅出一些東西。我必須以管理員身份運行它才能使其正常運行,但那很好。我走了這條路,因爲我可以在wireshark中看到數據包,但他們沒有進入我的udpclient。這最終成爲獲得我想要的最快方式。

 Socket ReceiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP); 
     EndPoint DefaultIPEndpoint = new IPEndPoint(IPAddress.Parse("10.0.2.0"), 0); 

     ReceiveSocket.ReceiveTimeout = 5000; 
     ReceiveSocket.Bind(DefaultIPEndpoint); 
     ReceiveSocket.IOControl(IOControlCode.ReceiveAll, new byte[4] { 1, 0, 0, 0 }, null); 

     while (true) 
     { 
      byte[] ReceiveBuffer = new byte[512]; 
      int ByteCount = 0; 

      ByteCount = ReceiveSocket.ReceiveFrom(ReceiveBuffer, ref DefaultIPEndpoint); 
      // Handle packets ... 
     } 
0

我看不到任何馬上出現錯誤的代碼,所以我沒有正確的答案給你。但是,這是我用來獲取UDP廣播消息的一段代碼。希望這會爲你工作,或給你一些新的想法:

class UdpHandler { 

    public UdpHandler(int portNo) { 
     Thread t = new Thread(ListenThread); 
     t.IsBackground = true; 
     t.Start(portNo); 
    } 

    public void ListenThread(object portNo) { 
     UdpClient client = new UdpClient { ExclusiveAddressUse = false }; 
     IPEndPoint localEp = new IPEndPoint(IPAddress.Any, (int)port); 
     client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); 
     client.ExclusiveAddressUse = false; 
     client.Client.Bind(localEp); 

     while (true) { 
      byte[] data = client.Receive(ref localEp); 
      DataReceived(data); 
     } 
    } 

    private void DataReceived(byte[] rawData) { 
     // Handle the received data 
    } 
} 
+0

是的,它沒有幫助我。我認爲廣播和多播消息之間的設置有所不同,但我不知道是什麼。仍然卡住。感謝您的幫助,但我很感激。 – shoyo