mirror of
https://gitee.com/johng/gf
synced 2026-07-03 03:39:35 +08:00
update example of gtcp
This commit is contained in:
23
geg/net/gtcp/server_client/gtcp_client.go
Normal file
23
geg/net/gtcp/server_client/gtcp_client.go
Normal file
@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/net/gtcp"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Client
|
||||
conn, err := gtcp.NewConn("127.0.0.1:8999")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
for i := 0; i < 3; i++ {
|
||||
if err := conn.Send([]byte(gconv.String(i))); err != nil {
|
||||
glog.Error(err)
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
24
geg/net/gtcp/server_client/gtcp_server.go
Normal file
24
geg/net/gtcp/server_client/gtcp_server.go
Normal file
@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/net/gtcp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Server
|
||||
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()
|
||||
}
|
||||
Reference in New Issue
Block a user