-
Notifications
You must be signed in to change notification settings - Fork 0
/
q2.go
143 lines (121 loc) · 3.1 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
package main
import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"net"
"net/http"
"os"
"time"
"github.com/valyala/fasthttp"
)
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]))
if string(buffer[:mLen]) == "http://localhost:8888/" {
_, err = connection.Write([]byte("Test!"))
} else if string(buffer[:mLen]) == "http://localhost:8888/test01" {
_, err = connection.Write([]byte("Test01!"))
} else if string(buffer[:mLen]) == "http://localhost:8888/test02" {
_, err = connection.Write([]byte("Test02!"))
} else {
_, err = connection.Write([]byte("Default!"))
}
connection.Close()
}