2017-08-11 115 views
0

我想從.NET核心MVC應用程序發送喚醒LAN數據包。無法發送喚醒與.NET核心,當在Ubuntu上的局域網

它完美的作品,如果應用程序在Windows主機上託管的,但是當它在我的樹莓派3運行(與Ubuntu伴侶),我收到以下錯誤:

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(); 
    } 

任何想法,就是這個原因的內容?應用程序的其餘部分按預期工作。

回答

3

它使節目播出後對我的作品:

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

謝謝了很多!那就是訣竅:-) – Pandora