mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
gtcp增加超时示例代码
This commit is contained in:
22
geg/net/gtcp/gtcp_timeout_client.go
Normal file
22
geg/net/gtcp/gtcp_timeout_client.go
Normal 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)
|
||||
}
|
||||
24
geg/net/gtcp/gtcp_timeout_server.go
Normal file
24
geg/net/gtcp/gtcp_timeout_server.go
Normal 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()
|
||||
}
|
||||
Reference in New Issue
Block a user