66 lines
933 B
Go
66 lines
933 B
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"net"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
serverAddress = "127.0.0.1:4242"
|
|
)
|
|
|
|
var (
|
|
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", serverAddress)
|
|
|
|
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()
|
|
}
|