简体   繁体   English

Lan唤醒-Linux-.Net核心

[英]Wake On Lan - Linux - .Net Core

I've made a small utility to send a magic packet to computers on my network to wake them. 我做了一个小工具,可以将魔术包发送到网络上的计算机以唤醒它们。 It works perfectly on Windows, but I get an ArgumentException when trying the same from a docker container (linux). 它在Windows上完美运行,但是从docker容器(linux)尝试执行此操作时却遇到ArgumentException

Can anyone shed any light on this? 谁能对此有所启示? Is it a bug in the UDP implementation? 这是UDP实现中的错误吗? Is it related to the docker networking not being able to reach a mac address on the same network as the host? 这与docker网络无法访问与主机位于同一网络上的mac地址有关吗?

My code: 我的代码:

public async Task<int> SendWakeOnLanAsync(byte[] mac)
{
    Log.Information("Waking on MAC: {mac}", BitConverter.ToString(mac));

    int counter = 0;

    byte[] bytes = new byte[6 * 17];
    for (var i = 0; i < 6; i++)
    {
        bytes[counter++] = 0xFF;
    }

    //16x MAC
    for (var i = 0; i < 16; i++)
    {
        mac.CopyTo(bytes, 6 + i * 6);
    }

    try
    {
        using (var client = new UdpClient())
        {
            client.EnableBroadcast = true;
            return await client.SendAsync(bytes, bytes.Length, new IPEndPoint(new IPAddress(0xffffffff), 0));
        }
    }
    catch (Exception ex)
    {
        Log.Error(ex, "Exception during wake on lan request.");
        throw;
    }
}

The ArgumentException trace: ArgumentException跟踪:

   at System.Net.Sockets.Socket.DoBeginSendTo(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, EndPoint endPointSnapshot, SocketAddress socketAddress, OverlappedAsyncResult asyncResult)
   at System.Net.Sockets.Socket.BeginSendTo(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, EndPoint remoteEP, AsyncCallback callback, Object state)
   at System.Net.Sockets.UdpClient.BeginSend(Byte[] datagram, Int32 bytes, IPEndPoint endPoint, AsyncCallback requestCallback, Object state)
   at System.Net.Sockets.UdpClient.<>c.<SendAsync>b__53_0(Byte[] targetDatagram, Int32 targetBytes, IPEndPoint targetEndpoint, AsyncCallback callback, Object state)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncImpl[TArg1,TArg2,TArg3](Func`6 beginMethod, Func`2 endFunction, Action`1 endAction, TArg1 arg1, TArg2 arg2, TArg3 arg3, Object state, TaskCreationOptions creationOptions)
   at System.Threading.Tasks.TaskFactory`1.FromAsync[TArg1,TArg2,TArg3](Func`6 beginMethod, Func`2 endMethod, TArg1 arg1, TArg2 arg2, TArg3 arg3, Object state)
   at System.Net.Sockets.UdpClient.SendAsync(Byte[] datagram, Int32 bytes, IPEndPoint endPoint)
   at Asgard.WakeOnLan.Api.EthernetClient.<SendWakeOnLanAsync>d__0.MoveNext() in /builds/smarthome/Asgard.WakeOnLan.Api/src/Asgard.WakeOnLan.Api/EthernetClient.cs:line 42
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Asgard.WakeOnLan.Api.Controllers.WakeOnLanController.<Wake>d__2.MoveNext() in /builds/smarthome/Asgard.WakeOnLan.Api/src/Asgard.WakeOnLan.Api/Controllers/WakeOnLanController.cs:line 38
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__27.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextActionFilterAsync>d__25.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextResourceFilter>d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResourceExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeAsync>d__20.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.<Invoke>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame`1.<RequestProcessingAsync>d__2.MoveNext()

This is because sending an IP package to port 0 isn't supported by all systems. 这是因为并非所有系统都支持将IP软件包发送到端口0。 Try using 7 or 9 instead (well-known but mostly unused ports). 尝试改用7或9(知名的但多数未使用的端口)。

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

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