Skip to content

Instantly share code, notes, and snippets.

@ekoecho
Created April 30, 2015 13:51
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ekoecho/c00dbc48b883799e76f7 to your computer and use it in GitHub Desktop.
Save ekoecho/c00dbc48b883799e76f7 to your computer and use it in GitHub Desktop.
package main
import (
"log"
"net"
)
func main() {
var status string
conn, err := net.Dial("tcp", "127.0.0.1:80")
if err != nil {
log.Println("Connection error:", err)
status = "Unreachable"
} else {
status = "Online"
defer conn.Close()
}
log.Println(status)
}
@fourcube
Copy link

This check will only work if someone is accepting connections on that port. If the port is taken but the holder does not accept connections the port is certainly unreachable, but not available for use.

You might want to use net.ListenTCP and try to bind to the address. This will fail when the port is already taken. This naturally only works for local checks.

@kovacshuni
Copy link

like

@akutz
Copy link

akutz commented Mar 10, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment