From ba3d264f0ccb2ca9a88dcf0e89304aef18a9cdf3 Mon Sep 17 00:00:00 2001 From: john Date: Tue, 25 Sep 2018 19:16:53 +0800 Subject: [PATCH] =?UTF-8?q?gtcp=E5=A2=9E=E5=8A=A0=E8=B6=85=E6=97=B6?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- geg/net/gtcp/gtcp_timeout_client.go | 22 ++++++++++++++++++++++ geg/net/gtcp/gtcp_timeout_server.go | 24 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 geg/net/gtcp/gtcp_timeout_client.go create mode 100644 geg/net/gtcp/gtcp_timeout_server.go diff --git a/geg/net/gtcp/gtcp_timeout_client.go b/geg/net/gtcp/gtcp_timeout_client.go new file mode 100644 index 000000000..c114ac4e4 --- /dev/null +++ b/geg/net/gtcp/gtcp_timeout_client.go @@ -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) +} \ No newline at end of file diff --git a/geg/net/gtcp/gtcp_timeout_server.go b/geg/net/gtcp/gtcp_timeout_server.go new file mode 100644 index 000000000..185f6b822 --- /dev/null +++ b/geg/net/gtcp/gtcp_timeout_server.go @@ -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() +} \ No newline at end of file