From 11ce706969a0734f233946110103b1dda36219d2 Mon Sep 17 00:00:00 2001 From: thesai <1021828630@qq.com> Date: Tue, 27 Feb 2024 10:26:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A8=A1=E6=8B=9F=E5=8D=8A?= =?UTF-8?q?=E5=AE=9E=E7=89=A9=E6=9C=8D=E5=8A=A1=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/mock_train_service/main.go | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/example/mock_train_service/main.go b/example/mock_train_service/main.go index 9cc6dbf..e360f7a 100644 --- a/example/mock_train_service/main.go +++ b/example/mock_train_service/main.go @@ -4,6 +4,8 @@ import ( "encoding/binary" "fmt" "net" + "os" + "strconv" "time" ) @@ -24,9 +26,23 @@ var trainRun bool // var IP = net.IPv4(192, 168, 3, 7) // var SendIP = net.IPv4(192, 168, 3, 7) var IP = net.IPv4(127, 0, 0, 1) -var SendIP = net.IPv4(127, 0, 0, 1) +var SendIP = "127.0.0.1" +var tick = 10 * time.Millisecond +// 参数1:ip 参数2:间隔 func main() { + if len(os.Args) >= 2 && os.Args[1] != "" { + SendIP = os.Args[0] + } + if len(os.Args) >= 3 && os.Args[2] != "" { + i, err := strconv.Atoi(os.Args[1]) + if err != nil { + panic(err) + } + tick = time.Duration(i) * time.Millisecond + } + fmt.Println("目的IP:", SendIP) + fmt.Println("间隔:", tick, "毫秒") listen, err := net.ListenUDP("udp", &net.UDPAddr{ IP: IP, Port: 5000, @@ -36,10 +52,10 @@ func main() { return } go func() { - tick := time.Tick(10 * time.Millisecond) - remoteAddr, err2 := net.Dial("udp", SendIP.String()+":"+"10000") - if err2 != nil { - fmt.Printf("Dial udp err: %v", err2) + tick := time.Tick(tick) + remoteAddr, err := net.Dial("udp", fmt.Sprintf("%s:%d", SendIP, 10000)) + if err != nil { + fmt.Printf("Dial udp err: %v", err) return } //remoteAddr := &net.UDPAddr{IP: SendIP, Port: 10000}