-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
49 lines (43 loc) · 807 Bytes
/
service.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
package main
import (
"fmt"
"net"
"os"
"time"
)
func main() {
addr := ":1200"
tcpAddr, err := net.ResolveTCPAddr("tcp4", addr)
checkError(err)
listener, err := net.ListenTCP("tcp", tcpAddr)
checkError(err)
for {
conn, err := listener.Accept()
if err != nil {
continue
}
go HandleClient(conn)
}
}
func HandleClient(conn net.Conn) {
conn.SetReadDeadline(time.Now().Add(1 * time.Minute))
request := make([]byte, 128)
defer conn.Close()
for {
read_len, _ := conn.Read(request)
fmt.Println(read_len)
fmt.Println(string(request[:read_len]))
if read_len == 0 {
break
} else {
conn.Write([]byte("see you again!"))
}
request = make([]byte, 128)
}
}
func checkError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "error:%s", err.Error())
os.Exit(1)
}
}