UDP has no handshake, so how do you tell an open port from a dead one?

Dialing a UDP port always succeeds, so a naive check says every port is up. How to get a real answer: send a request the service replies to, and catch ICMP port unreachable on a connected socket.

· 3 min read · By the Spot Downtime team

My first UDP check reported every port on the internet as up, including ones with nothing listening. The reason is one line of Go:

conn, err := net.Dial("udp", "example.com:27015") // err is nil for any host and port

A UDP “dial” sends nothing. It picks a local port and remembers where to send. With no handshake, silence from a port looks the same whether the service is fine, dead, behind a firewall, or the packet got lost.

The only proof is a reply

So the check can't just knock. It sends a request the service will answer, and counts the port as up only when a reply comes back containing the bytes you expect. Some that work:

  • NTP (port 123): the byte 1b followed by 47 zero bytes. Any NTP server answers with the time.
  • Source game servers: the A2S_INFO query, FF FF FF FF 54 then Source Engine Query and a null byte. Require only the FF FF FF FF prefix, because newer servers reply with a challenge first.
  • Your own service: whatever ping or status message it already answers.

Closed ports can fail in milliseconds

Waiting for a reply means a dead service costs a full timeout. But when a packet reaches a host with nothing on that port, the host usually sends back ICMP “port unreachable”. Whether your program ever hears about it depends on the socket:

connected socket (net.Dial)            unconnected socket (net.ListenPacket)
  Write  -> packet to host:port          WriteTo -> packet to host:port
  <- ICMP port unreachable               <- ICMP port unreachable
  Read   -> ECONNREFUSED, right away      ReadFrom -> waits until the timeout

On a connected socket the kernel matches the ICMP error to it and returns it from the next call. So the check writes first, then reads, and tells the two failures apart:

n, err := conn.Read(buf)
switch {
case errors.Is(err, syscall.ECONNREFUSED):
	res.Error = "port closed (ICMP port unreachable)"
case isTimeout(err):
	res.Error = "no reply within 10s"
}

“Port closed” means the host is up and your service isn't running. “No reply” means something dropped the packets or the service is hung. Different problems, different fixes.

Over UDP only a reply proves the service is alive. A closed port fails fast; silence waits for the timeout.

Silence still isn't proof

Firewalls drop ICMP and hosts rate-limit it, so a closed port can look exactly like a lost packet. A single missed reply shouldn't page anyone: the check is retried and confirmed from a second region before an incident opens.

Have a real protocol? Check that instead

For a DNS server, resolving a real record says far more than “something answered on port 53”. Raw UDP checks are for game servers, VoIP, NTP and custom protocols.

In short

  • Over UDP, a port is up only if it answers.
  • Use a connected socket, so closed ports fail fast instead of timing out.
  • Confirm a timeout before you alert.

I wrote this while building UDP checks for Spot Downtime.

Get new guides by email

Practical posts on monitoring, outages, error fixes and on-call, when they're published. No spam.

Unsubscribe from any email with one click. See our privacy policy.

Keep reading