-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtcp_socket.go
More file actions
173 lines (155 loc) · 3.96 KB
/
tcp_socket.go
File metadata and controls
173 lines (155 loc) · 3.96 KB
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
package main
import (
"bytes"
"errors"
"fmt"
"io"
"math"
"math/rand"
"net"
"strconv"
"sync"
"time"
)
const (
SERVER_NETWORK = "tcp"
SERVER_ADDRESS = "127.0.0.1:8888"
DELIMITER = '\t'
)
var wg sync.WaitGroup
var logSn = 1
func printLog(format string, args ...interface{}) {
fmt.Printf("%d: %s", logSn, fmt.Sprintf(format, args...))
logSn++
}
func convertToInt32(str string) (int32, error) {
num, err := strconv.Atoi(str)
if err != nil {
printLog(fmt.Sprintf("Parse Error: %s\n", err))
return 0, errors.New(fmt.Sprintf("'%s' is not integer!", str))
}
if num > math.MaxInt32 || num < math.MinInt32 {
printLog(fmt.Sprintf("Convert Error: The integer %s is too large/small.\n", num))
return 0, errors.New(fmt.Sprintf("'%s' is not 32-bit integer!", num))
}
return int32(num), nil
}
func cbrt(param int32) float64 {
return math.Cbrt(float64(param))
}
func read(conn net.Conn) (string, error) {
readBytes := make([]byte, 1)
var buffer bytes.Buffer
for {
_, err := conn.Read(readBytes)
if err != nil {
return "", err
}
readByte := readBytes[0]
if readByte == DELIMITER {
break
}
buffer.WriteByte(readByte)
}
return buffer.String(), nil
}
func write(conn net.Conn, content string) (int, error) {
var buffer bytes.Buffer
buffer.WriteString(content)
buffer.WriteByte(DELIMITER)
return conn.Write(buffer.Bytes())
}
func clientGo(id int) {
defer wg.Done()
conn, err := net.DialTimeout(SERVER_NETWORK, SERVER_ADDRESS, 2*time.Second)
if err != nil {
printLog("Dial Error: %s (Client[%d])\n", err, id)
return
}
defer conn.Close()
printLog("Connected to server. (remote address: %s, local address: %s) (Client[%d])\n", conn.RemoteAddr(), conn.LocalAddr(), id)
time.Sleep(200 * time.Millisecond)
requestTimes := 5
conn.SetDeadline(time.Now().Add(5 * time.Millisecond))
for i := 0; i < requestTimes; i++ {
i32Req := rand.Int31()
n, err := write(conn, fmt.Sprintf("%d", i32Req))
if err != nil {
printLog("Write Error: %s (Client[%d])\n", err, id)
continue
}
printLog("Sent request (written %d bytes): %d (Client[%d])\n", n, i32Req, id)
}
for j := 0; j < requestTimes; j++ {
strResp, err := read(conn)
if err != nil {
if err == io.EOF {
printLog("The connection is closed by another side. (Client[%d])\n", id)
} else {
printLog("Read Error: %s (Client[%d])\n", err, id)
}
break
}
printLog("Received response: %s (Client[%d])\n", strResp, id)
}
}
func serverGo() {
var listener net.Listener
listener, err := net.Listen(SERVER_NETWORK, SERVER_ADDRESS)
if err != nil {
printLog("Listen Error: %s\n", err)
return
}
defer listener.Close()
printLog("Got listener for the server. (local address: %s)\n", listener.Addr())
for {
conn, err := listener.Accept()
if err != nil {
printLog("Accept Error: %s\n", err)
}
printLog("Established a connection with a client application. (remote address: %s)\n", conn.RemoteAddr())
go handleConn(conn)
}
}
func handleConn(conn net.Conn) {
defer func() {
conn.Close()
wg.Done()
}()
for {
conn.SetReadDeadline(time.Now().Add(10 * time.Second))
strReq, err := read(conn)
if err != nil {
if err == io.EOF {
printLog("The connection is closed by another side. (Server)\n")
} else {
printLog("Read Error: %s (Server)\n", err)
}
break
}
printLog("Received request: %s (Server)\n", strReq)
i32Req, err := convertToInt32(strReq)
if err != nil {
n, err := write(conn, err.Error())
if err != nil {
printLog("Write Error (written %d bytes): %s (Server)\n", err)
}
printLog("Sent response (written %d bytes): %s (Server)\n", n, err)
continue
}
f64Resp := cbrt(i32Req)
respMsg := fmt.Sprintf("The cube root of %d is %f.", i32Req, f64Resp)
n, err := write(conn, respMsg)
if err != nil {
printLog("Write Error: %s (Server)\n", err)
}
printLog("Sent response (written %d bytes): %s (Server)\n", n, respMsg)
}
}
func main() {
wg.Add(2)
go serverGo()
time.Sleep(500 * time.Millisecond)
go clientGo(1)
wg.Wait()
}