在PowerShell中实现UDP网络发现

时间:2017-10-20 21:46:57

标签: powershell sockets udp

我正在尝试在PowerShell中实现UDP广播/响应网络发现协议。我在C#中找到了一些这样做的例子,但我在转换到PowerShell时遇到了麻烦。

协议非常简单:

  1. 从端口x向端口x发送广播。
  2. 节点使用有效负载回复端口x。
  3. 将发件人IP地址解析为构建列表。
  4. 到目前为止,我有部分广播工作。我可以发送到相应的端口,但我没有任何运气来定义源端口。我知道它有效,因为我在Wireshark中看到了响应。我现在正在改变原始端口。我正在尝试获取接收开始的代码块,而我却试图翻译C#代码。我猜测Power Shell可以做到这一点(因为它与C#非常相似),但我还没有看到它在任何地方都说过。

    C#示例(exampleexampleexampleexampleexample),VB.NET example,Java {{3 }}

    我开始尝试从C#转换此Example(带有一些code)并且我收到运行时错误(此处为代码):

    $Enc      = [System.Text.Encoding]::ASCII 
    $Message = "WPPS" *10 
    $responseData   = $Enc.GetBytes( $Message ) 
    
    #$responseData = [System.Text.Encoding]::ASCIIGetBytes("someData");
    
    while (1)
    {
        $server = New-Object System.Net.Sockets.UdpClient(1047);
        $clientEp = New-Object System.Net.IPEndPoint(0.0.0.0, 1047);
        $clientRequestData = server.Receive(ref $clientEp);
        $clientRequest = Encoding.ASCII.GetString($clientRequestData);
    
        Console.WriteLine($"Recived {$clientRequest} from {$clientEp.Address}, sending response: {$responseData}");
        $server.Send($responseData, $responseData.Length, $clientEp);
        $server.Close();
    }
    

    当我尝试运行上面的代码时,我会在下面重复出现错误。我确定这是C#和PowerShell语法之间的差异,但我几乎不知道它可能是什么。我不确定第一个错误是在第一个迭代循环之后,还是在第一个迭代之后。它看起来像是。

    New-Object : Exception calling ".ctor" with "1" argument(s): "Only one usage of each socket address (protocol/network 
    address/port) is normally permitted"
    At line:9 char:15
    +     $server = New-Object System.Net.Sockets.UdpClient(1047);
    +               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
        + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
    
    New-Object : Exception calling ".ctor" with "2" argument(s): "Value cannot be null.
    Parameter name: address"
    At line:10 char:17
    +     $clientEp = New-Object System.Net.IPEndPoint(0.0.0.0, 1047);
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
        + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
    
    ref : The term 'ref' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of 
    the name, or if a path was included, verify that the path is correct and try again.
    At line:11 char:41
    +     $clientRequestData = server.Receive(ref $clientEp);
    +                                         ~~~
        + CategoryInfo          : ObjectNotFound: (ref:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException
    
    Encoding.ASCII.GetString : The term 'Encoding.ASCII.GetString' is not recognized as the name of a cmdlet, function, script file, 
    or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:12 char:22
    +     $clientRequest = Encoding.ASCII.GetString($clientRequestData);
    +                      ~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (Encoding.ASCII.GetString:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException
    
    $Recived {$clientRequest} from {$clientEp.Address}, sending response: {$responseData} : The term '$Recived {$clientRequest} from 
    {$clientEp.Address}, sending response: {$responseData}' is not recognized as the name of a cmdlet, function, script file, or 
    operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:14 char:23
    + ... e.WriteLine($"Recived {$clientRequest} from {$clientEp.Address}, send ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: ($Recived {$clie...{$responseData}:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException
    
    Exception calling "Send" with "3" argument(s): "A request to send or receive data was disallowed because the socket is not 
    connected and (when sending on a datagram socket using a sendto call) no address was supplied"
    At line:15 char:5
    +     $server.Send($responseData, $responseData.Length, $clientEp);
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : SocketException
    

    在Power Shell中是否有任何关于如何执行此操作的示例? Power Shell能否实际做到这一点?

    我在Power Shell中了解了几件事,但几乎没有关于C#的事情。

0 个答案:

没有答案