63 lines
959 B
Go
Raw Normal View History

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