Created
March 10, 2019 09:10
-
-
Save wisecsj/7fb5148c40f539ba0b1725c9adbb30d5 to your computer and use it in GitHub Desktop.
client version 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"net" | |
"time" | |
) | |
// Client launch the client | |
func Client() { | |
addr, _ := net.ResolveUDPAddr("udp", "localhost:8000") | |
c, err := net.DialUDP("udp", nil, addr) | |
if err != nil { | |
panic("Dial failed ...") | |
} | |
// RTT := make([]int, 10) | |
maxRTT := 0 | |
minRTT := 0x3f3f3f3f | |
const total int = 10 | |
// packet超时时间 | |
const mtime = time.Second * 3 | |
lossNum := 0 | |
for i := 0; i < total; i++ { | |
start := time.Now() | |
msg := fmt.Sprintf("Ping %d %s", i, start) | |
c.Write([]byte(msg)) | |
buf := make([]byte, 1024) | |
// c.Read(buf) | |
// log.Printf("%s", buf) | |
c.SetReadDeadline(time.Now().Add(time.Second)) | |
_, err := c.Read(buf) | |
if err != nil { | |
if timeErr := err.(net.Error); timeErr != nil && timeErr.Timeout() == true { | |
log.Println("数据包接收超时...") | |
lossNum++ | |
continue | |
} | |
} | |
duration := time.Since(start) | |
d := int(duration / time.Millisecond) | |
if d > maxRTT { | |
maxRTT = d | |
} | |
if d < minRTT { | |
minRTT = d | |
} | |
log.Printf("sucess:ping %d;RTT:%d ms", i, d) | |
} | |
log.Println("-------------------") | |
log.Printf("loss:%d%%|maxRTT:%d ms|minRTT:%d ms", lossNum*100/total, maxRTT, minRTT) | |
} | |
func main() { | |
Client() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment