Checking a remote udp port

Good day.
I am checking the availability and responsiveness of a remote udp port:

$Udpclient=new-Object system.Net.Sockets.Udpclient
$Udpclient.client.ReceiveTimeout=20000
$Udpclient.Connect('',) 
$AsciiEncoder = new-object system.text.asciiencoding 
$byte = $AsciiEncoder.GetBytes("Anybody there?") 
$Udpclient.Send($byte,$byte.length) 
$IPEndPoint = New-Object system.net.ipendpoint([system.net.ipaddress]::Any,0) 
$ReceiveBytes = $Udpclient.Receive([ref]$IPEndPoint) 
$AsciiEncoder.GetString($ReceiveBytes)
$Udpclient.close()

When i debug it i can see that all is correct until this line is executed:
$ReceiveBytes = $Udpclient.Receive([ref]$IPEndPoint)
It times out after 20 sec with the following exception:
Exception calling “Receive” with “1” argument(s): “A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond”

When i test the availability of the same port with Microsoft’s port query utility i get success in 5 sec:
portqry.exe -n servername -e portnumber -p UDP

i also tried the Test-Port PS script and it failed.
i am puzzled.
Can you help?

Regards

There’s an ancient saying: when in doubt, use Process Monitor.

I get a syntax error with this line:

$Udpclient.Connect('',)

+ $Udpclient.Connect('',)
+                       ~
Missing expression after ','.

You’re trying to connect to any address port 0?

$IPEndPoint = New-Object system.net.ipendpoint([system.net.ipaddress]::Any,0) 

See if this guidance helps, that is if you have not already seen it.

Querying UDP Ports with PowerShell | Learn Powershell | Achieve More
Querying UDP Ports with PowerShell: Part 2 | Learn Powershell | Achieve More

$Udpclient.Connect('',)

was a typo, sorry. it should have read

$Udpclient.Connect(servername, portnumber)

substitute servername with a UNC server name and portnumber with udp port on that server.

as to this line

$IPEndPoint = New-Object system.net.ipendpoint([system.net.ipaddress]::Any,0)

it is used in practically all examples as a broad range endpoint which allows communication to come from all ip addresses and all ports.

All the best.

Yes, Postanote, i read both articles. In fact the code that i put together is basically a scalleton code from one of the examples.

All the best.

The test-port here function worked for me (Browse code samples | Microsoft Docs), at least with one of Comcast’s dns servers. I guess it depends on if the udp server responds to garbage. It’s not like you can test making a syn connection with tcp. The code at the bottom of Querying UDP Ports with PowerShell: Part 2 | Learn Powershell | Achieve More didn’t work for me. Strange, sending “TEST” to the Comcast dns server gets no response, but “Anybody there?” does. test-port sends the date.

PS /Users/js/Downloads> Test-Port -comp cdns01.comcast.net -port 53 -udp -verbose

VERBOSE: Making UDP connection to remote server
VERBOSE: Sending message to remote host
VERBOSE: Creating remote endpoint
VERBOSE: Waiting for message return
VERBOSE: Connection Successful


Server   : cdns01.comcast.net
Port     : 53
TypePort : UDP
Open     : True
Notes    : 07??

Also osx has nc (netcat):

PS /Users/js> nc -zuv cdns01.comcast.net 53                       
                                                
found 0 associations
found 1 connections:
     1:	flags=82
	outif (null)
	src 192.168.1.108 port 50158
	dst 75.75.75.75 port 53
	rank info not available

Connection to cdns01.comcast.net port 53 [udp/domain] succeeded!

Actually your code works for me. The 14 is from updclient.send() and asciiencoder.getstring() returns the weird characters.

$Udpclient = new-Object system.Net.Sockets.Udpclient
$Udpclient.client.ReceiveTimeout = 1000
$Udpclient.Connect('cdns01.comcast.net',53)
$AsciiEncoder = new-object system.text.asciiencoding
$byte = $AsciiEncoder.GetBytes("Anybody there?")
$Udpclient.Send($byte,$byte.length)
$IPEndPoint = New-Object system.net.ipendpoint([system.net.ipaddress]::Any,0)
$ReceiveBytes = $Udpclient.Receive([ref]$IPEndPoint)
$AsciiEncoder.GetString($ReceiveBytes)
$Udpclient.close()

14
An??

Good day.
I’ve tinkered a bit more.
$Udpclient.Connect opens a local listening udp port and sets accordingly the $Udpclient.client.localendpoint
So after $Udpclient.Connect one can see among udp ports the one that $Udpclient.client.localendpoint describes.
I went to the remote server and tried to portqry.exe that port on my local machine and failed.
With that i declared the problem a cold case.
As to asciiencoder.getstring() returning weird characters it seems to me that you hit upon a service that takes whatever string you send to it and leaves only first two and the last character.

All the best.