-
Notifications
You must be signed in to change notification settings - Fork 0
/
q2.go
185 lines (157 loc) · 3.98 KB
/
q2.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net"
"net/http"
"os"
"time"
"github.com/valyala/fasthttp"
)
type User1 struct {
Name1 string `json:"text1"`
}
type User2 struct {
Name2 string `json:"text2"`
}
type User3 struct {
Name3 string `json:"text3"`
}
type User4 struct {
Name4 string `json:"text4"`
}
type MyResponse struct {
Name string
}
const (
SERVER_HOST = "localhost"
SERVER_PORT = "8888"
SERVER_TYPE = "tcp"
)
func main() {
server := func() {
// Server
fmt.Println("Server Running...")
server, err := fasthttp.ListenAndServe(SERVER_TYPE, SERVER_HOST+":"+SERVER_PORT)
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}
defer server.Close()
fmt.Println("Listening on " + SERVER_HOST + ":" + SERVER_PORT)
fmt.Println("Waiting for client...")
for {
connection, err := server.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
}
fmt.Println("client connected")
go processClient(connection)
}
}
client1 := func() {
// Client 1
//establish connection
rand.Seed(time.Now().UnixNano())
connection, err := net.Dial(SERVER_TYPE, SERVER_HOST+":"+SERVER_PORT)
if err != nil {
panic(err)
}
///send some data
_, err = connection.Write([]byte("http://localhost:8888/"))
buffer := make([]byte, 1024)
mLen, err := connection.Read(buffer)
if err != nil {
fmt.Println("Error reading:", err.Error())
}
fmt.Println("Received for Client 1: ", string(buffer[:mLen]))
rand1_sleep := rand.Intn(6) + 2
time.Sleep(time.Duration(rand1_sleep) * time.Second)
resp, err := http.Get("http://localhost:8888/")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(body))
defer connection.Close()
}
client2 := func() {
// Client 2
//establish connection
rand.Seed(time.Now().UnixNano())
connection, err := net.Dial(SERVER_TYPE, SERVER_HOST+":"+SERVER_PORT)
if err != nil {
panic(err)
}
///send some data
_, err = connection.Write([]byte("http://localhost:8888/test01"))
buffer := make([]byte, 1024)
mLen, err := connection.Read(buffer)
if err != nil {
fmt.Println("Error reading:", err.Error())
}
fmt.Println("Received for Client 2: ", string(buffer[:mLen]))
rand2_sleep := rand.Intn(6) + 2
time.Sleep(time.Duration(rand2_sleep) * time.Second)
resp, err := http.Get("http://localhost:8888/")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(body))
defer connection.Close()
}
go server()
go client1()
go client2()
}
func processClient(connection net.Conn) {
buffer := make([]byte, 1024)
mLen, err := connection.Read(buffer)
if err != nil {
fmt.Println("Error reading:", err.Error())
}
fmt.Println("Received for Server: ", string(buffer[:mLen]))
u1 := User1{}
u2 := User2{}
u3 := User3{}
u4 := User4{}
b1 := []byte(`{"text1": "Test!"}`)
b2 := []byte(`{"text2": "Test01!"}`)
b3 := []byte(`{"text3": "Test02!"}`)
b4 := []byte(`{"text4": "Default!"}`)
err1 := json.Unmarshal(b1, &u1)
err2 := json.Unmarshal(b2, &u2)
err3 := json.Unmarshal(b3, &u3)
err4 := json.Unmarshal(b4, &u4)
fmt.Println(err1)
fmt.Println(err2)
fmt.Println(err3)
fmt.Println(err4)
if string(buffer[:mLen]) == "http://localhost:8888/" {
var myResponse1 = MyResponse{Name: u1.Name1}
_, err = connection.Write([]byte(myResponse1.Name))
} else if string(buffer[:mLen]) == "http://localhost:8888/test01" {
var myResponse2 = MyResponse{Name: u2.Name2}
_, err = connection.Write([]byte(myResponse2.Name))
} else if string(buffer[:mLen]) == "http://localhost:8888/test02" {
var myResponse3 = MyResponse{Name: u3.Name3}
_, err = connection.Write([]byte(myResponse3.Name))
} else {
var myResponse4 = MyResponse{Name: u4.Name4}
_, err = connection.Write([]byte(myResponse4.Name))
}
connection.Close()
}