linux - 在 ubuntu 上无法使用 .NET Core 在 LAN 上发送唤醒

标签 linux ubuntu asp.net-core .net-core udpclient

我正在尝试从 .NET Core MVC 应用程序发送局域网唤醒数据包。

如果应用程序托管在 Windows 主机上,它可以完美运行,但是当它在我的 Raspberry Pi 3(带有 Ubuntu Mate)上运行时,我收到以下错误:

fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0]
An unhandled exception has occurred: Permission denied 255.255.255.255:40000
System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (0x80004005): Permission denied 255.255.255.255:40000
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
at System.Net.Sockets.Socket.Connect(IPAddress address, Int32 port)

我正在使用它来发送 WOL 数据包:

public static bool WakeUp(string macAddress)
    {
        bool retVal = false;
        try
        {
            byte[] mac = null;

            // Parse string to byte array
            string[] macDigits = null;
            if (macAddress.Contains("-"))
            {
                macDigits = macAddress.Split('-');
            }
            else
            {
                macDigits = macAddress.Split(':');
            }

            if (macDigits.Length != 6)
            {
                //incorrect MAC address
            }
            else
            {
                mac = macDigits.Select(s => Convert.ToByte(s, 16)).ToArray();
            }

            if (mac != null)
            {

                // WOL packet is sent over UDP 255.255.255.0:40000.
                UdpClient client = new UdpClient();
                client.Client.Connect(IPAddress.Broadcast, 40000);

                // WOL packet contains a 6-bytes trailer and 16 times a 6-bytes sequence containing the MAC address.
                byte[] packet = new byte[17 * 6];

                // Trailer of 6 times 0xFF.
                for (int i = 0; i < 6; i++)
                    packet[i] = 0xFF;
                // Body of magic packet contains 16 times the MAC address.
                for (int i = 1; i <= 16; i++)
                    for (int j = 0; j < 6; j++)
                        packet[i * 6 + j] = mac[j];

                // Submit
                int result = client.Client.Send(packet);
                if (result > 0)
                {
                    retVal = true;
                }
            }
        }
        catch (Exception ex)
        {
            throw;
        }

        return retVal;
    }

出于测试目的,我将端口更改为 2050 -> 同样的错误。

为了以防万一,这是 Program.cs 的内容

public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseKestrel()
            .UseUrls("http://*:80")
            .UseContentRoot(Directory.GetCurrentDirectory())
            .Build();
    }

知道这是什么原因吗?应用程序的其余部分按预期工作。

最佳答案

启用广播后对我有用:

// UdpClient client = new UdpClient();
client.EnableBroadcast = true;

关于linux - 在 ubuntu 上无法使用 .NET Core 在 LAN 上发送唤醒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45642356/

相关文章:

java - 无法创建文件 [META-INF/war-tracker] Tomcat 9 linux

php - 连接到在 Ubuntu AWS 实例中创建的 MySQL 数据库

java - 安卓工作室 3.1.4 : missing classes and uncorrect Blueprint displaying

javascript - 如何在 Angular 员工 component.html 上返回 EmployeeCode?

asp.net-mvc - 在我们使用 JWT token 的情况下,Asp.net 核心中的 session 和应用程序变量(因此没有基于 cookie 的 session )

linux - 如何在链接时自动发现隐式依赖

python - 安装spacy失败

eclipse - 无法使用 eclipse、apache2 和 tomcat7 (Ubuntu) 访问我的 webapp

asp.net - dotnet 发布和 web.config?

linux - "Couldn' t lock/home/marc/sent?”在 Mutt 中