correctly quit when connection fails

This commit is contained in:
Norwin Roosen 2019-01-11 17:22:51 +01:00
parent 80756ae8c2
commit f07d2de3a8
1 changed files with 9 additions and 3 deletions

12
main.go
View File

@ -31,6 +31,13 @@ func main() {
log.Fatal("No image or no server address provided") 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 // Start cpu profiling if wanted
if *cpuprofile != "" { if *cpuprofile != "" {
f, err := os.Create(*cpuprofile) f, err := os.Create(*cpuprofile)
@ -58,16 +65,15 @@ func bomb(messages []byte) {
conn, err := net.Dial("tcp", *address) conn, err := net.Dial("tcp", *address)
if err != nil { 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() defer conn.Close()
// Start bombardement // Start bombardement
for { for {
_, err := conn.Write(messages) _, err := conn.Write(messages)
if err != nil { if err != nil {
log.Println(err.Error()) log.Fatal(err)
} }
} }
} }