Last active
May 18, 2019 08:29
-
-
Save falzm/8b7f1e71ff9f30476a9b000e3fe4926e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net" | |
"os" | |
"time" | |
) | |
var ( | |
host string | |
port string | |
) | |
func init() { | |
if len(os.Args) != 3 { | |
log.Fatalf("usage: %s <host> <port>", os.Args[0]) | |
} | |
host = os.Args[1] | |
port = os.Args[2] | |
} | |
func main() { | |
addrs, err := net.LookupHost(host) | |
if err != nil { | |
log.Fatalf("unable to resolve address for host %q: %s", host, err) | |
} | |
if len(addrs) == 0 { | |
log.Fatal("no address found for host %q", host) | |
} | |
for { | |
t := time.Now() | |
conn, err := net.Dial("tcp", net.JoinHostPort(addrs[0], port)) | |
if err != nil { | |
log.Fatalf("%s", err) | |
} | |
latency := time.Now().Sub(t) | |
conn.Close() | |
log.Printf("%s[%s]:%s\t%s\n", host, addrs[0], port, latency) | |
time.Sleep(time.Second) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage: