我有一個產品直接插入到我的計算機的以太網端口,發送多播數據包到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;
}
}
}
}
}
是的,它沒有幫助我。我認爲廣播和多播消息之間的設置有所不同,但我不知道是什麼。仍然卡住。感謝您的幫助,但我很感激。 – shoyo