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
$AsciiEncoder = new-object system.text.asciiencoding 
$byte = $AsciiEncoder.GetBytes("Anybody there?") 
$IPEndPoint = New-Object[]::Any,0) 
$ReceiveBytes = $Udpclient.Receive([ref]$IPEndPoint) 

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?


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

I get a syntax error with this line:


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

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

$IPEndPoint = New-Object[]::Any,0) 

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


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[]::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 (, 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 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 -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   :
Port     : 53
TypePort : UDP
Open     : True
Notes    : 07??

Also osx has nc (netcat):

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

Connection to 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
$AsciiEncoder = new-object system.text.asciiencoding
$byte = $AsciiEncoder.GetBytes("Anybody there?")
$IPEndPoint = New-Object[]::Any,0)
$ReceiveBytes = $Udpclient.Receive([ref]$IPEndPoint)


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.