2018-07-20 11:41:23 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-04-03 00:03:46 +08:00
|
|
|
"fmt"
|
|
|
|
"github.com/gogf/gf/g/net/gtcp"
|
|
|
|
"github.com/gogf/gf/g/os/glog"
|
|
|
|
"github.com/gogf/gf/g/util/gconv"
|
|
|
|
"time"
|
2018-07-20 11:41:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2019-04-03 00:03:46 +08:00
|
|
|
// Server
|
|
|
|
go gtcp.NewServer("127.0.0.1:8999", func(conn *gtcp.Conn) {
|
|
|
|
defer conn.Close()
|
|
|
|
for {
|
|
|
|
data, err := conn.Recv(-1)
|
|
|
|
if len(data) > 0 {
|
|
|
|
fmt.Println(string(data))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
// client closed, err will be: EOF
|
|
|
|
fmt.Println(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).Run()
|
2018-07-20 11:41:23 +08:00
|
|
|
|
2019-04-03 00:03:46 +08:00
|
|
|
time.Sleep(time.Second)
|
2018-07-20 11:41:23 +08:00
|
|
|
|
2019-04-03 00:03:46 +08:00
|
|
|
// Client
|
|
|
|
conn, err := gtcp.NewConn("127.0.0.1:8999")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
for i := 0; i < 10000; i++ {
|
|
|
|
if err := conn.Send([]byte(gconv.String(i))); err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
if i == 5 {
|
|
|
|
conn.Close()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-03-03 20:20:31 +08:00
|
|
|
|
2019-04-03 00:03:46 +08:00
|
|
|
// exit after 5 seconds
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
}
|