Multicast Listner error

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Multicast Listner error

Post by sekou2331 »

His so I wrote this code and I keep getting error that I just cant seem to figure out. It is not happening in case though. Maybe it is something that I am not seeing. I am getting the error below.
Exception calling "ReceiveFrom" with "2" argument(s): "You must call the Bind method before performing this operation."
At D:\Script2.ps1:294 char:4
+ $receivedBytes = $mCastSocket.ReceiveFrom($bytes, [ref]$remEndPoint)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidOperationException



Code: Select all

function Multicast-Listen([int]$Port, [string]$IPMCastAddress, [string]$IPLocalAddress){


  
  #First create the multicast socket.
  $mCastSocket = New-Object System.Net.Sockets.Socket([System.Net.Sockets.AddressFamily]::InterNetwork, [System.Net.Sockets.SocketType]::Dgram, [System.Net.Sockets.ProtocolType]::Udp)
  
  #Now create the local endpoint using the address and port passed in.
  $locEndPoint = New-Object System.Net.IPEndPoint([System.Net.IPAddress]::Parse($IPLocalAddress), $Port)
  
  #Create the remote endpoint (any)
  $remEndPoint = New-Object System.Net.IPEndPoint([System.Net.IPAddress]::Parse([Net.IPAddress]::Any),0)
  #I'm not sure why, but if you don't cast this to an EndPoint object, it won't work
  $remEndPoint = [System.Net.EndPoint] $remEndPoint
  
  #Define our MulticastOption object (mcast address etc.)
  $mCastOption = New-Object System.Net.Sockets.MulticastOption([System.Net.IPAddress]::Parse($IPMCastAddress), [System.Net.IPAddress]::Parse($IPLocalAddress))
  
  $mCastSocket.SetSocketOption([System.Net.Sockets.SocketOptionLevel]::IP, [System.Net.Sockets.SocketOptionName]::AddMembership, $mCastOption)
  
  #Bind the socket to the endpoint.
  $mCastSocket.Bind($locEndPoint)
  
  #We need an array of bytes.
  [byte[]]$bytes = 0..255|%{0}

   $enc = New-Object System.Text.ASCIIEncoding
   $receivedBytes = $mCastSocket.ReceiveFrom($bytes, [ref]$remEndPoint)
   $str = $enc.GetString($bytes, 0, $receivedBytes)
   # add output to an array 


  #Write to host.
write-host  "Waiting for a connection on $IPMCastAddress $port $IPLocalAddress"
$mCastSocket.Close()
       
        write-host "Received multicast from $($remEndPoint.ToString()) $($str)"
         write-host  -Message "Connection closed." 
}

Multicast-Listen -IPMCastAddress 233.0.0.0  -Port 11111 -IPLocalAddress 127.0.0.0

jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multicast Listner error

Post by jvierra »

Somewhere you are supplying bad values. Perhaps the port is in use.

Try running at a prompt one line at a time until you see your error.

You cannot use 127.0.0.1 as a local address.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Multicast Listner error

Post by sekou2331 »

I know the local IP is wrong I just added that as filer. I think I see it now. I added the below lines. Seems to fix the issue.



  1.  
  2. $mCastSocket.ExclusiveAddressUse = false;
  3.   $mCastSocket.SetSocketOption([System.Net.Sockets.SocketOptionLevel]::Socket, [System.Net.Sockets.SocketOptionName]::ReuseAddress, $true)
  4.  
  5.  
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Multicast Listner error

Post by sekou2331 »

So this is my last question. I am noticing it sticking. I think it is because of it some sources not having multicast data. There is a ReceiveTimeout. Can you give me an example on how this would work. I never could get it to work properly. Or is there another way to time out and move to the next multicast connection?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multicast Listner error

Post by jvierra »

I recommend that you look at the documentation for the class you are trying to use. The docs will show you how you can manage the class.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multicast Listner error

Post by jvierra »

I should point out that we normally use ReceiveFromAsync events to receive data from a listening port.

https://msdn.microsoft.com/en-us/librar ... .110).aspx
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Multicast Listner error

Post by sekou2331 »

I am sorry are you saying the way I am using the ReiceveTimeout is wrong? Or are you saying the way I am approaching my whole code is incorrect?


  1. function Multicast-Listen([int]$Port, [string]$IPMCastAddress, [string]$IPLocalAddress){
  2.  
  3. #First create the multicast socket.
  4.   $mCastSocket = New-Object System.Net.Sockets.Socket([System.Net.Sockets.AddressFamily]::InterNetwork, [System.Net.Sockets.SocketType]::Dgram, [System.Net.Sockets.ProtocolType]::Udp)
  5.  
  6.   #Now create the local endpoint using the address and port passed in.
  7.   $locEndPoint = New-Object System.Net.IPEndPoint([System.Net.IPAddress]::Parse($IPLocalAddress), $Port)
  8.  
  9.   #Create the remote endpoint (any)
  10.   $remEndPoint = New-Object System.Net.IPEndPoint([System.Net.IPAddress]::Parse([Net.IPAddress]::Any),0)
  11.   #I'm not sure why, but if you don't cast this to an EndPoint object, it won't work
  12.   $remEndPoint = [System.Net.EndPoint] $remEndPoint
  13.  
  14.   #Define our MulticastOption object (mcast address etc.)
  15.  
  16.   $mCastOption = New-Object System.Net.Sockets.MulticastOption([System.Net.IPAddress]::Parse($IPMCastAddress), [System.Net.IPAddress]::Parse($IPLocalAddress))
  17.   $mCastSocket.SetSocketOption([System.Net.Sockets.SocketOptionLevel]::Socket, [System.Net.Sockets.SocketOptionName]::ReuseAddress, $true)
  18.   $mCastSocket.SetSocketOption([System.Net.Sockets.SocketOptionLevel]::IP, [System.Net.Sockets.SocketOptionName]::AddMembership, $mCastOption)
  19.  
  20.   #Bind the socket to the endpoint.
  21.   $mCastSocket.Bind($locEndPoint)
  22.     $mCastSocket.ReceiveTimeout = 3000
  23.   #We need an array of bytes.
  24.   [byte[]]$bytes = 0..255|%{0}
  25.  
  26.    $enc = New-Object System.Text.ASCIIEncoding
  27.    $receivedBytes = $mCastSocket.ReceiveFrom($bytes, [ref]$remEndPoint)
  28.    $str = $enc.GetString($bytes, 0, $receivedBytes)
  29.    # add output to an array
  30.  
  31.  
  32.   #Write to host.
  33. write-host  "Waiting for a connection on $IPMCastAddress $port $IPLocalAddress"
  34. $mCastSocket.Close()
  35.        
  36.         write-host "Received multicast from $($remEndPoint.ToString()) $($str)"
  37.          write-host  -Message "Connection closed."
  38. }
  39.  
  40. Multicast-Listen -IPMCastAddress 233.0.0.0  -Port 11111 -IPLocalAddress 100.2.3.4
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multicast Listner error

Post by jvierra »

Where did I post that. I am only noting that the usual way to receive data is with an async callback. If you use the timeout then you will have to run in a loop.
This topic is 6 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked