gtcp增加超时示例代码

This commit is contained in:
john
2018-09-25 19:16:53 +08:00
parent 91b316eabf
commit ba3d264f0c
2 changed files with 46 additions and 0 deletions

View File

@ -0,0 +1,22 @@
package main
import (
"gitee.com/johng/gf/g/net/gtcp"
"gitee.com/johng/gf/g/os/glog"
"gitee.com/johng/gf/g/os/gtime"
"time"
)
func main() {
conn, err := gtcp.NewConn("127.0.0.1:8999")
if err != nil {
panic(err)
}
defer conn.Close()
if err := conn.Send([]byte(gtime.Now().String())); err != nil {
glog.Error(err)
}
time.Sleep(time.Minute)
}

View File

@ -0,0 +1,24 @@
package main
import (
"fmt"
"time"
"gitee.com/johng/gf/g/net/gtcp"
)
func main() {
gtcp.NewServer("127.0.0.1:8999", func(conn *gtcp.Conn) {
defer conn.Close()
conn.SetRecvDeadline(time.Now().Add(10*time.Second))
for {
data, err := conn.Recv(-1)
fmt.Println(err)
if len(data) > 0 {
fmt.Println(string(data))
}
if err != nil {
break
}
}
}).Run()
}