说明
UDP组播。
组播使用
组播使用非常简单,
- 配置中启用广播UseBroadcast重要
- 在UdpSession启动后。调用JoinMulticastGroup即可加入组播。调用DropMulticastGroup,退出组播。
下列示例使用的是UdpPackageAdapter适配器,如果是普通udp接受端,请使用NormalUdpDataHandlingAdapter适配器。
private static void TestUdpMulticastGroup()
{
IPEndPoint multicast = new IPEndPoint(IPAddress.Parse("224.5.6.7"), 7788);
UdpSession mainUdpSession = new UdpSession();
mainUdpSession.SetDataHandlingAdapter(new UdpPackageAdapter());
//udpSession.SetDataHandlingAdapter(new NormalUdpDataHandlingAdapter());
mainUdpSession.Setup(new RRQMConfig()
.SetBindIPHost(new IPHost(7789))
.SetThreadCount(1)
.UseBroadcast()
.SetBufferLength(1024 * 64))
.Start();
mainUdpSession.JoinMulticastGroup(IPAddress.Parse("224.5.6.7"));
UdpSession multicastUdpSession = new UdpSession();
multicastUdpSession.SetDataHandlingAdapter(new UdpPackageAdapter());
//udpSession.SetDataHandlingAdapter(new NormalUdpDataHandlingAdapter());
int count = 0;
multicastUdpSession.Received += (remote, byteBlock, requestInfo) =>
{
Console.WriteLine(byteBlock.ToString());
count++;
if (count==5)
{
multicastUdpSession.DropMulticastGroup(IPAddress.Parse("224.5.6.7"));
}
};
multicastUdpSession.Setup(new RRQMConfig()
.SetBindIPHost(new IPHost(7788))
.SetThreadCount(1)
.SetBufferLength(1024 * 64))
.Start();
multicastUdpSession.JoinMulticastGroup(IPAddress.Parse("224.5.6.7"));
while (true)
{
mainUdpSession.Send(multicast,Encoding.UTF8.GetBytes("RRQM"));
Thread.Sleep(1000);
}
}
广播
广播的使用也非常简单,仅仅需要将发送的目的地址设置为255.255.255.255即可。此处可通过IPEndPoint实现,也可以通过IPHost实现。