I am looking for a simple way to see if a remote UDP port is open on a server
-
possible duplicate of [UDP port open check C#](http://stackoverflow.com/questions/5879605/udp-port-open-check-c-sharp) – Joe Feb 13 '12 at 18:15
-
4@Joe that question is definitely something different... the method in that answer does NOT apply to remote serverrs only to the local machine ! – Yahia Feb 13 '12 at 18:18
-
udp doesn't send the AWK response that tcp/ip does...there has to be an initial handshake in there somewhere. try sending the port some data and see what happens. – Paul Nikonowicz Feb 13 '12 at 18:27
-
Connect the socket to the target and send more than one datagram to it. If you don't get any exceptions either the port is closed at the firewall or there is something bound to it in the host. – user207421 Jun 28 '20 at 03:29
2 Answers
BEWARE that several firewalls/network setups/IDS etc. might influence the result... the following method is NOT 100% reliable but it is the only method possible with UDP IMHO (since UDP is connectionless).
You can use UdpClient
, set a receive timeout on the underlying socket, make a connection to that remote server/port, Send
some small message (byte[]
!) and call Receive
.
IF the port is closed you get an exception saying that the connection was forcibly closed (SocketException
with ErrorCode
10054 = WSAECONNRESET
)... which means the port is NOT open.
Otherwise you either receive an answer OR a timeout exception - both should be interpreted as "the UDP port is open".

- 69,653
- 9
- 115
- 144
-
@ralu I know it is reddiculous but that's how that exception is actually phrased... – Yahia Feb 13 '12 at 18:31
-
I am sure it is, but is still funny. UdpClient is than application protocol over UDP? – Luka Rahne Feb 13 '12 at 18:33
-
@ralu `UdpClient` is a built-in class which makes a nice wrapper around Socket for UDP handling... – Yahia Feb 13 '12 at 18:35
-
@ralu added the exact WinSocket Error code (10054) which is available in the Exception for that case... – Yahia Feb 13 '12 at 18:36
-
@Yahia Hi, I'm trying to do the same thing, but in my case, I always receive a SocketErrorCode == SocketError.TimedOut for closed ports(or inexisting hosts). Is there a workaround? – J4N Apr 26 '13 at 10:46
-
1@J4N inexisting hosts is something different from a closed port... there is NO 100% way to remotely diagnose UDP ports... what exactly is your goal ? – Yahia Apr 26 '13 at 10:51
-
@Yahia: We are developing an application(composed of client and servers), and most of our customers have routers that doesn't allow ICMP(ping). The idea was then to check if we have one of our service running on the remote machine. Unfortunately, we can't add a method on service since we have a lot of different service type. – J4N Apr 26 '13 at 11:02
-
-
@J4N you situation seems a bit complicated... I would create 1 "central management service" where all services actively register themselves when they start, send "heartbeats" and unregister when they shutdown... – Yahia Apr 26 '13 at 14:12
-
2I tried it on everything in my lan and i always get a Timeout, whether the port is open or not. too bad. – i3arnon May 13 '14 at 13:40
You can not. That is by design because UDP is connectionless. You have to solve that on application layer.

- 10,336
- 3
- 34
- 56