-
Notifications
You must be signed in to change notification settings - Fork 5
/
bc.go
67 lines (57 loc) · 1.17 KB
/
bc.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
package main
import (
"io/ioutil"
"os"
"net"
"fmt"
"strconv"
"time"
)
func main() {
// reading file and keeping it in memory for sending it
// as a batch of data
buffer, err := ioutil.ReadFile(os.Args[3])
if err != nil {
fmt.Println("Unable to read given file", err.Error())
return
}
conn_count, err := strconv.Atoi(os.Args[2])
if err != nil {
fmt.Println("Connections count should be valid number", err.Error())
return
}
for i := 0; i < conn_count; i++ {
go run_connection(os.Args[1], buffer)
}
// just waiting until exit
for {
time.Sleep(time.Second * 100)
}
}
func run_connection(address string, buffer []byte) {
addr, err := net.ResolveTCPAddr("tcp", address)
if err != nil {
fmt.Println("Unable to resolve address", err.Error())
return
}
conn, err := net.DialTCP("tcp", nil, addr)
if err != nil {
fmt.Println("Unable to connect", err.Error())
return
}
// just to free up socket buffer sent by echo server
readable_buffer := make([]byte, 64000)
go func() {
for {
_, err = conn.Read(readable_buffer)
if err != nil {
conn.Close()
return
}
}
}()
for {
conn.Write(buffer)
time.Sleep(time.Millisecond * 100)
}
}