简体   繁体   English

广播单声道与核心

[英]Broadcast-Mono vs Core

A strange thing I am observing on Linux. The following simple code is broadcasting through the whole LAN.我在 Linux 上观察到一件奇怪的事情。下面的简单代码通过整个 LAN 进行广播。 I compile that simple project as Core project and/or Mono project (both for Linux).我将该简单项目编译为核心项目和/或 Mono 项目(均适用于 Linux)。 They seem to work normally, but only from Mono compilation I am able to catch UDP packets otherwise not (with Core).它们似乎工作正常,但只有从 Mono 编译我才能捕获 UDP 数据包,否则不能(使用 Core)。

public sealed class Broadcaster : IDisposable
{
   readonly IPEndPoint _ipEndPoint;
   readonly object _locker = new object();
   readonly Dictionary<string, UdpClient> _clients = new Dictionary<string, UdpClient>();

   public Broadcaster(ushort broadcastPort)
   {
       _ipEndPoint = new IPEndPoint(IPAddress.Broadcast, broadcastPort);
   }

   public void Send(string message)
   {
       var data = Encoding.ASCII.GetBytes(message);
       var host = Dns.GetHostEntry(Dns.GetHostName());
       var addressList = host.AddressList;
       var ip = addressList.Last();
       var ip_ = ip.ToString();
       lock (_locker)
       {
           if (!_clients.ContainsKey(ip_))
               _clients.Add(ip_, new UdpClient(new IPEndPoint(ip, 0)));
       }
       try
       {
           _clients[ip_].SendAsync(data, data.Length, _ipEndPoint);
           Console.WriteLine($"broadcast: {DateTime.UtcNow}");
       }
       catch (Exception ex)
       {
           Console.WriteLine($"unable to use host {ip} while broadcasting: " + ex);
       }
   }

   public void Dispose()
   {
       try
       {
           lock (_locker)
           {
               foreach (var client in _clients)
               {
                   client.Value.Close();
                   client.Value.Dispose();
               }
               _clients.Clear();
           }
       }
       catch (Exception ex)
       {
           Console.WriteLine("Network boadcaster finishing: " + ex);
       }
   }
}  

...and its usage: ...及其用法:

static async Task Main(string[] args)
{
    var broadcaster = new Broadcaster(15015);
    while (true)
    {
        broadcaster.Send($"Broadcaster: { DateTime.UtcNow.ToString("HH:mm:ss.fff")}");
        await Task.Delay(1000);
    }
}

Already checked (bash - no graphical UI on Linux):已经检查过(bash - Linux 上没有图形用户界面):

  • ufw allow 15005/udp ufw 允许 15005/udp
  • The other computer in addition to Broadcast receiving app (which catches fine the packets for Mono broadcaster) is now equiped with Wireshark that just confirmng my observation.除了广播接收应用程序(它可以很好地捕获 Mono 广播公司的数据包)之外的另一台计算机现在配备了 Wireshark,这正好证实了我的观察。

Please, what is the problem with the Core version of Broadcaster?请问核心版Broadcaster有什么问题? Do I need to change it, or tell Linux "hey this app is allowed to broadcast, or what"?我是否需要更改它,或者告诉 Linux“嘿这个应用程序允许广播,或者什么”? What is Mono doing differently than Core? Mono 与 Core 有何不同?

Different implementations set different default values when talking to native socket API. So, in your case, you should set UdpClient.EnableBroadcast to true so that .NET Core can send out broadcast requests.与本机套接字 API 通话时,不同的实现设置不同的默认值。因此,在您的情况下,您应该将UdpClient.EnableBroadcast设置为true ,以便 .NET Core 可以发送广播请求。

Reference参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM