Simulate multi simultaneous connexions to tcp server

Hello,
I need to test if a Tcp server handles multi clients connexions correctly.
Is it possible to use goroutines like this?

func SendMsg(msg string) {
conn, _ := net.Dial(“tcp”, “:1123”)
conn.Write([]byte(msg))
conn.Close()
}

for i := 0; i < 50; i++ {
go SendMsg(“test”)
}

when I try something like this, the program panics.

Regards

What panic?

Go: panic: runtime error: invalid memory address or nil pointer dereference

Perhaps check the err returned by net.Dial, it’s returned for a reason…

Norbert beat me to it: it does sound like your conn is nil, like here: https://play.golang.org/p/mJSIcJLDpTD. Remove the return and it will panic.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.