Skip to content

Instantly share code, notes, and snippets.

@jasonberanek
Created August 14, 2014 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonberanek/badec0f4a1e4ee880b06 to your computer and use it in GitHub Desktop.
Save jasonberanek/badec0f4a1e4ee880b06 to your computer and use it in GitHub Desktop.
Go code used to investigate message across platforms for connecting to an address that is unavailable
import (
"log"
"net"
"syscall"
"time"
)
func main() {
address := "127.0.0.1:5901"
log.Printf("Trying address: %s...", address)
l, err := net.DialTimeout("tcp", address, 2*time.Second)
if err == nil {
log.Printf("%s in use", address)
l.Close()
} else if e, ok := err.(*net.OpError); ok {
if e.Err == syscall.ECONNREFUSED {
// then port should be available for listening
log.Printf("ECONNREFUSED when connecting to: %s\n\t%v", address, e.Err)
} else if e.Timeout() {
log.Printf("Timeout connecting to: %s (check firewall rules)\n\t%v", address, e.Err)
} else {
log.Printf("Error when connecting to: %s\n\t%v", address, e.Err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment