-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
121 lines (100 loc) · 1.82 KB
/
main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"encoding/json"
"fmt"
"log"
)
type Traffic struct {
Listen []string
Talk []Talk
}
type Talk struct {
Proto string
Address string
Port int
Rate int
Data string
}
func main(){
jsonData := []byte(`
{
"listen": ["8080","8081"],
"talk": [
{
"proto": "tcp",
"address": "127.0.0.1",
"port": 8081,
"rate": 1000,
"data": "hello world"
},
{
"proto": "tcp",
"address": "127.0.0.1",
"port": 8080,
"rate": 1000,
"data": "hello world"
}
]
}
`)
var traffic Traffic
err := json.Unmarshal([]byte(jsonData), &traffic)
if err != nil {
log.Fatalf("Unable to marshal JSON due to %s", err)
}
for _, v := range traffic.Listen {
fmt.Printf("starting server on port: %s\n", v)
go tcpServer(v)
}
for _, v := range traffic.Talk {
fmt.Printf("talking on port: %d\n", v.Port)
go tcpClient("127.0.0.1", "8080", "testdata", 1000)
}
select{}
}
/* package main
import (
"io"
"net"
"os"
)
const (
HOST = "localhost"
PORT = "3333"
TYPE = "tcp"
)
func main() {
tcpServer, err := net.ResolveTCPAddr(TYPE, HOST+":"+PORT)
if err != nil {
println("ResolveTCPAddr failed:", err.Error())
os.Exit(1)
}
conn, err := net.DialTCP(TYPE, nil, tcpServer)
if err != nil {
println("Dial failed:", err.Error())
os.Exit(1)
}
defer conn.Close()
_, err = conn.Write([]byte("Ground Control To Major Tom"))
if err != nil {
println("Write data failed:", err.Error())
os.Exit(1)
}
//new line of code
conn.CloseWrite()
received := make([]byte, 4096)
for {
println("Reading data...")
temp := make([]byte, 4096)
_, err = conn.Read(temp)
if err != nil {
if err == io.EOF {
break
}
println("Read data failed:", err.Error())
os.Exit(1)
}
received = append(received, temp...)
}
println("Received message:", string(received))
} */