Skip to content

Instantly share code, notes, and snippets.

@wisecsj
Last active March 10, 2019 09:09
Show Gist options
  • Save wisecsj/cde649a32001a9186586a899002b944e to your computer and use it in GitHub Desktop.
Save wisecsj/cde649a32001a9186586a899002b944e to your computer and use it in GitHub Desktop.
client version 1
package main
import (
"fmt"
"log"
"net"
"net/http"
_ "net/http/pprof"
"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.0
minRTT := 0.0
const total int = 10
// packet超时时间
const mtime = time.Second * 3
lossNum := 0
for i := 0; i < total; i++ {
ch := make(chan bool)
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)
go func() {
c.Read(buf)
ch <- true
}()
select {
case <-ch:
case <-time.After(mtime):
lossNum++
log.Printf("Faild: Ping %d,cost %f s", i, float64(time.Since(start)/time.Second))
continue
}
duration := time.Since(start)
d := float64(duration / time.Millisecond)
if d > maxRTT {
maxRTT = d
}
if d < minRTT {
minRTT = d
}
log.Printf("sucess:ping %d;RTT:%f ms", i, d)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment