本文整理汇总了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();
}
示例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();
}
示例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);
}
}
示例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);
}
}
示例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();
}
示例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));
}
}
}