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.
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
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
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?
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.