当前位置: 首页>>代码示例>>C#>>正文


C# UdpClient.DropMulticastGroup方法代码示例

本文整理汇总了C#中System.Net.Sockets.UdpClient.DropMulticastGroup方法的典型用法代码示例。如果您正苦于以下问题:C# UdpClient.DropMulticastGroup方法的具体用法?C# UdpClient.DropMulticastGroup怎么用?C# UdpClient.DropMulticastGroup使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Sockets.UdpClient的用法示例。


在下文中一共展示了UdpClient.DropMulticastGroup方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: HandleBroadcasts

        private void HandleBroadcasts()
        {
            var localIP = new IPEndPoint(IPAddress.Any, 50001);
            var udpClient = new UdpClient();
            udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            udpClient.ExclusiveAddressUse = false;
            udpClient.Client.Bind(localIP);

            var multicastingIP = IPAddress.Parse("228.5.6.7");
            udpClient.JoinMulticastGroup(multicastingIP);

            running = true;
            while (running)
            {
                var bytes = udpClient.Receive(ref localIP);
                var message = Encoding.UTF8.GetString(bytes);

                Dispatcher.Invoke(() =>
                {
                    lblResponse.Content = message;
                });
            }

            udpClient.DropMulticastGroup(multicastingIP);
            udpClient.Close();
        }
开发者ID:bdr27,项目名称:c-,代码行数:26,代码来源:MainWindow.xaml.cs

示例2: BoardbastMessages

        private void BoardbastMessages()
        {
            var multicastIP = IPAddress.Parse("228.5.6.7");
            var udpClient = new UdpClient();
            udpClient.JoinMulticastGroup(multicastIP);
            var endPoint = new IPEndPoint(multicastIP, 50001);
            running = true;

            while (running)
            {
                Thread.Sleep(2000);

                //broadbast the current time to all client
                var nowText = DateTime.Now.ToShortTimeString();
                var bytes = Encoding.UTF8.GetBytes(nowText);
                udpClient.Send(bytes, bytes.Length, endPoint);
            }

            udpClient.DropMulticastGroup(multicastIP);
            udpClient.Close();
        }
开发者ID:bdr27,项目名称:c-,代码行数:21,代码来源:MainWindow.xaml.cs

示例3: BackgroundListener

        private void BackgroundListener()
        {
            IPEndPoint bindingEndpoint = new IPEndPoint(IPAddress.Any, _endPoint.Port);
            using (UdpClient client = new UdpClient())
            {
                client.ExclusiveAddressUse = false;
                client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                client.Client.Bind(bindingEndpoint);
                client.JoinMulticastGroup(_endPoint.Address);

                bool keepRunning = true;
                while (keepRunning)
                {
                    try
                    {
                        IPEndPoint remote = new IPEndPoint(IPAddress.Any, _endPoint.Port);
                        byte[] buffer = client.Receive(ref remote);
                        lock (this)
                        {
                            DataReceived(this, new MulticastDataReceivedEventArgs(remote, buffer));
                        }
                    }
                    catch (ThreadAbortException)
                    {
                        keepRunning = false;
                        Thread.ResetAbort();
                    }
                }

                client.DropMulticastGroup(_endPoint.Address);
            }
        }
开发者ID:Fuzzbawls,项目名称:MinerControl-KBomba,代码行数:32,代码来源:MulticastReceiver.cs

示例4: Sender

        private static async Task Sender(IPEndPoint endpoint, bool broadcast, string groupAddress)
        {
            try
            {
                string localhost = Dns.GetHostName();
                using (var client = new UdpClient())
                {
                    client.EnableBroadcast = broadcast;
                    if (groupAddress != null)
                    {
                        client.JoinMulticastGroup(IPAddress.Parse(groupAddress));
                    }

                    bool completed = false;
                    do
                    {
                        WriteLine("Enter a message or bye to exit");
                        string input = ReadLine();
                        WriteLine();
                        completed = input == "bye";

                        byte[] datagram = Encoding.UTF8.GetBytes($"{input} from {localhost}");
                        int sent = await client.SendAsync(datagram, datagram.Length, endpoint);
                    } while (!completed);

                    if (groupAddress != null)
                    {
                        client.DropMulticastGroup(IPAddress.Parse(groupAddress));
                    }

                }
            }
            catch (SocketException ex)
            {
                WriteLine(ex.Message);
            }

        }
开发者ID:ProfessionalCSharp,项目名称:ProfessionalCSharp6,代码行数:38,代码来源:Program.cs

示例5: threadProc

        private void threadProc()
        {
            IPAddress addr = IPAddress.Parse(address);
            IPEndPoint ep = new IPEndPoint(addr, port);
            client = new UdpClient(port);

            while (!isTerminated)
            {
                byte[] data = client.Receive(ref ep);
                string s = Encoding.UTF8.GetString(data);
                this.SetText(s);
                vport.Write(s);
            }

            client.DropMulticastGroup(addr);
            client.Close();
        }
开发者ID:2m0nd,项目名称:udp2com,代码行数:17,代码来源:UDPMulticastListener.cs

示例6: ReaderAsync

        private static async Task ReaderAsync(int port, string groupAddress)
        {
            using (var client = new UdpClient(port))
            {
                if (groupAddress != null)
                {
                    client.JoinMulticastGroup(IPAddress.Parse(groupAddress));
                    WriteLine($"joining the multicast group {IPAddress.Parse(groupAddress)}");
                }

                bool completed = false;
                do
                {
                    WriteLine("starting the receiver");
                    UdpReceiveResult result = await client.ReceiveAsync();
                    byte[] datagram = result.Buffer;
                    string received = Encoding.UTF8.GetString(datagram);
                    WriteLine($"received {received}");
                    if (received == "bye")
                    {
                        completed = true;
                    }
                } while (!completed);
                WriteLine("receiver closing");

                if (groupAddress != null)
                {
                    client.DropMulticastGroup(IPAddress.Parse(groupAddress));
                }
            }
        }
开发者ID:ProfessionalCSharp,项目名称:ProfessionalCSharp6,代码行数:31,代码来源:Program.cs


注:本文中的System.Net.Sockets.UdpClient.DropMulticastGroup方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。