-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtestwrite.go
90 lines (74 loc) · 1.63 KB
/
testwrite.go
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
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"encoding/base64"
"fmt"
"io/ioutil"
"math/rand"
"net"
"os"
"strconv"
"time"
)
func main() {
if len(os.Args) < 3 {
help()
os.Exit(1)
}
filename := os.Args[1]
dst := os.Args[2]
count := 1
if len(os.Args) > 3 {
var err error
count, err = strconv.Atoi(os.Args[3])
if err != nil {
exitByErr(err)
}
}
cnt, err := ioutil.ReadFile(filename)
if err != nil {
exitByErr(err)
}
conn, err := net.Dial("tcp", dst)
if err != nil {
exitByErr(err)
}
defer conn.Close()
for index, b := range cnt {
cnt[index] = b ^ 0xff
}
buf := []byte(base64.StdEncoding.EncodeToString(cnt))
fmt.Printf("total len: %d\n", len(buf)*count)
rand.Seed(time.Now().Unix())
for i := 0; i < count; i++ {
l := len(buf)
avail := l
offset := 0
used := l
for avail > 0 {
if avail <= 1 {
used = avail
} else {
used = rand.Intn(avail-1) + 1
}
wb := buf[offset : offset+used]
offset += used
avail -= used
n, err := conn.Write(wb)
if err != nil {
exitByErr(err)
}
fmt.Printf("write %d bytes to %s\n", n, dst)
if avail > 0 {
time.Sleep(time.Second / 100)
}
}
}
}
func help() {
fmt.Println("usage: put filename host:port [count]")
}
func exitByErr(err error) {
fmt.Printf("ERR: %s\n\n", err)
help()
os.Exit(1)
}