62 lines
990 B
Go
62 lines
990 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")
|
||
|
message = []byte{1}
|
||
|
)
|
||
|
|
||
|
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(50 * time.Millisecond) // Adjust the sleep duration as needed
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
var wg sync.WaitGroup
|
||
|
|
||
|
// Start multiple UDP clients in separate goroutines
|
||
|
for i := 0; i < *numClients; i++ {
|
||
|
wg.Add(1)
|
||
|
go udpClient(&wg)
|
||
|
}
|
||
|
|
||
|
// Signal all client goroutines to stop
|
||
|
wg.Wait()
|
||
|
}
|