C# 局域网唤醒-Linux-.Net内核
我已经做了一个小实用程序发送一个魔术包到我的网络上的计算机唤醒他们。它在Windows上运行得非常好,但是当我在docker容器(linux)上尝试同样的操作时,我会得到一个C# 局域网唤醒-Linux-.Net内核,c#,linux,docker,.net-core,wake-on-lan,C#,Linux,Docker,.net Core,Wake On Lan,我已经做了一个小实用程序发送一个魔术包到我的网络上的计算机唤醒他们。它在Windows上运行得非常好,但是当我在docker容器(linux)上尝试同样的操作时,我会得到一个ArgumentException 有人能解释一下吗?这是UDP实现中的错误吗?这是否与docker网络无法在与主机相同的网络上访问mac地址有关 我的代码: public async Task<int> SendWakeOnLanAsync(byte[] mac) { Log.Information("
ArgumentException
有人能解释一下吗?这是UDP实现中的错误吗?这是否与docker网络无法在与主机相同的网络上访问mac地址有关
我的代码:
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;
}
}
这是因为并非所有系统都支持向端口0发送IP包。尝试改用7或9(众所周知但大多未使用的端口)。这是因为并非所有系统都支持向端口0发送IP包。尝试改用7或9(众所周知但大部分未使用的端口)。尝试使用
--net=host
和/或--privileged
运行容器。Docker在一个单独的网络堆栈中运行您的容器,默认情况下,它的功能少于root在主机上的功能。公平地说,这是UDP实现中的一个缺陷吗?这是一个记录良好、经过尝试和测试的数据报包,可以追溯到1980年,我怀疑这是一个缺陷。试着看看你的代码,看看问题出在哪里。乍一看,这很明显,6+i*6
这是否意味着,6+(i*6)vs(6+i)*6?能够使用端口0重新编程(与端口一起工作!=0),我实际上得到了一个aggregateeexception
封装了SocketException
@t0mm13b:UDP经过了良好的测试,但Linux上的.NET内核肯定不是。由于它可以在Windows上运行,所以我假设在不同平台上的实现至少存在差异。它是6+(i*6)
。我可以从Windows唤醒电脑,所以我对这一点很有信心。@BMitch:Spot on。除了Martin对异常的修复(将端口更改为7),--net=host确保数据包被发送到正确的位置--特权阶层没有。谢谢尝试使用--net=host
和/或--privileged
运行容器。Docker在一个单独的网络堆栈中运行您的容器,默认情况下,它的功能少于root在主机上的功能。公平地说,这是UDP实现中的一个缺陷吗?这是一个记录良好、经过尝试和测试的数据报包,可以追溯到1980年,我怀疑这是一个缺陷。试着看看你的代码,看看问题出在哪里。乍一看,这很明显,6+i*6
这是否意味着,6+(i*6)vs(6+i)*6?能够使用端口0重新编程(与端口一起工作!=0),我实际上得到了一个aggregateeexception
封装了SocketException
@t0mm13b:UDP经过了良好的测试,但Linux上的.NET内核肯定不是。由于它可以在Windows上运行,所以我假设在不同平台上的实现至少存在差异。它是6+(i*6)
。我可以从Windows唤醒电脑,所以我对这一点很有信心。@BMitch:Spot on。除了Martin对异常的修复(将端口更改为7),--net=host确保数据包被发送到正确的位置--特权阶层没有。谢谢答对 了它仍然不起作用,但我将其归因于docker网络。但是更改端口删除了exception.Bingo。它仍然不起作用,但我将其归因于docker网络。但是更改端口可以消除异常。
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()