2018-07-04 19:32:51 +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/os/gtime"
|
|
|
|
"time"
|
2018-07-04 19:32:51 +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 {
|
|
|
|
if err := conn.Send(append([]byte("> "), data...)); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).Run()
|
2018-07-04 19:32:51 +08:00
|
|
|
|
2019-04-03 00:03:46 +08:00
|
|
|
time.Sleep(time.Second)
|
2018-07-04 19:32:51 +08:00
|
|
|
|
2019-04-03 00:03:46 +08:00
|
|
|
// Client
|
|
|
|
for {
|
|
|
|
if conn, err := gtcp.NewPoolConn("127.0.0.1:8999"); err == nil {
|
|
|
|
if b, err := conn.SendRecv([]byte(gtime.Datetime()), -1); err == nil {
|
|
|
|
fmt.Println(string(b), conn.LocalAddr(), conn.RemoteAddr())
|
|
|
|
} else {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
} else {
|
|
|
|
glog.Error(err)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}
|