-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
52 lines (50 loc) · 1.1 KB
/
server.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
package main
import (
"fmt"
"github.com/lim-yoona/tcpack"
"net"
"time"
)
func main() {
address, err := net.ResolveTCPAddr("tcp4", ":8099")
if err != nil {
fmt.Println("create address failed")
}
fmt.Println(*address)
listener, err := net.ListenTCP("tcp4", address)
if err != nil {
fmt.Println("create listener failed")
}
for {
tcpConn, err := listener.AcceptTCP()
if err != nil {
fmt.Println("return conn failed")
}
mp := tcpack.NewMsgPack(8, tcpConn)
go func() {
for {
msg, err := mp.Unpack()
if err != nil {
fmt.Println("read msg failed:", err)
} else {
fmt.Printf("读取到了%d个字符\n", msg.GetDataLen())
fmt.Println("内容为:", (string(msg.GetMsgData())))
}
time.Sleep(time.Second * 5)
}
}()
go func() {
for {
var data string
fmt.Scanln(&data)
msg := tcpack.NewMessage(0, uint32(len([]byte(data))), []byte(data))
num, err := mp.Pack(msg)
if err != nil {
fmt.Println("msg send failed:", err)
}
fmt.Printf("发送了%d个字符\n", num)
fmt.Printf("内容为:%s(%s)\n", []byte(data), data)
}
}()
}
}