From f07d2de3a8a16707d148b9718e239a98c2dfd2a0 Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Fri, 11 Jan 2019 17:22:51 +0100 Subject: [PATCH] correctly quit when connection fails --- main.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 667a7d6..af10771 100644 --- a/main.go +++ b/main.go @@ -31,6 +31,13 @@ func main() { log.Fatal("No image or no server address provided") } + // check connectivity by opening one test connection + conn, err := net.Dial("tcp", *address) + if err != nil { + log.Fatal(err) + } + conn.Close() + // Start cpu profiling if wanted if *cpuprofile != "" { f, err := os.Create(*cpuprofile) @@ -58,16 +65,15 @@ func bomb(messages []byte) { conn, err := net.Dial("tcp", *address) if err != nil { - log.Print("error establishing tcp connection: " + err.Error()) + log.Fatal(err) } - //TODO: Actually close the connection and not just terminate main thread defer conn.Close() // Start bombardement for { _, err := conn.Write(messages) if err != nil { - log.Println(err.Error()) + log.Fatal(err) } } }