2024-01-25 12:10:29 +01:00

63 lines
959 B
Go

package main
import (
"flag"
"fmt"
"net"
"sync"
"time"
)
var (
address = flag.String("a", "127.0.0.1:4242", "address (host and port)")
numClients = flag.Int("c", 10, "number of clients")
sleepTime = flag.Duration("s", time.Second, "sleep time for each client")
message = []byte{1, 0}
)
func init() {
flag.Parse()
}
func udpClient(wg *sync.WaitGroup) {
defer wg.Done()
clientAddr, err := net.ResolveUDPAddr("udp", *address)
if err != nil {
fmt.Println("Error resolving UDP address:", err)
return
}
conn, err := net.DialUDP("udp", nil, clientAddr)
if err != nil {
fmt.Println("Error connecting to UDP server:", err)
return
}
defer conn.Close()
for {
_, err := conn.Write(message)
if err != nil {
fmt.Println("Error sending message:", err)
return
}
time.Sleep(*sleepTime)
}
}
func main() {
wg := sync.WaitGroup{}
for i := 0; i < *numClients; i++ {
wg.Add(1)
go udpClient(&wg)
}
wg.Wait()
}