-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
78 lines (61 loc) · 1.4 KB
/
Copy pathclient.go
File metadata and controls
78 lines (61 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"context"
"crypto/tls"
"flag"
"io"
"log"
"strconv"
"time"
quic "github.com/lucas-clemente/quic-go"
)
func main() {
hostName := flag.String("hostname", "localhost", "hostname/ip of the server")
portNum := flag.String("port", "4242", "port number of the server")
numEcho := flag.Int("necho", 100, "number of echos")
timeoutDuration := flag.Int("rtt", 50, "timeout duration (in ms)")
flag.Parse()
addr := *hostName + ":" + *portNum
tlsConf := &tls.Config{
InsecureSkipVerify: true,
NextProtos: []string{"quic-echo"},
}
session, err := quic.DialAddr(addr, tlsConf, nil)
if err != nil {
panic(err)
}
stream, err := session.OpenStreamSync(context.Background())
if err != nil {
panic(err)
}
counter := 0
timeout := time.Duration(*timeoutDuration) * time.Millisecond
resp := make(chan string)
for {
message := strconv.Itoa(counter)
counter++
log.Printf("Client: Sending '%s'\n", message)
_, err = stream.Write([]byte(message))
if err != nil {
panic(err)
}
log.Println("Done. Waiting for echo")
go func() {
buff := make([]byte, len(message))
_, err = io.ReadFull(stream, buff)
if err != nil {
panic(err)
}
resp <- string(buff)
}()
select {
case reply := <-resp:
log.Printf("Client: Got '%s'\n", reply)
case <-time.After(timeout):
log.Printf("Client: Timed out\n")
}
if counter == *numEcho {
break
}
}
}