1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| package main
import ( "fmt" "io" "net" "time" "github.com/aceld/zinx/znet" )
func main() {
fmt.Println("Client Test ... start") time.Sleep(3 * time.Second)
conn,err := net.Dial("tcp", "127.0.0.1:7777") if err != nil { fmt.Println("client start err, exit!") return }
for n := 3; n >= 0; n-- { dp := znet.NewDataPack() msg, _ := dp.Pack(znet.NewMsgPackage(0,[]byte("Zinx Client Test Message"))) _, err := conn.Write(msg) if err !=nil { fmt.Println("write error err ", err) return }
headData := make([]byte, dp.GetHeadLen()) _, err = io.ReadFull(conn, headData) if err != nil { fmt.Println("read head error") break } msgHead, err := dp.Unpack(headData) if err != nil { fmt.Println("server unpack err:", err) return }
if msgHead.GetDataLen() > 0 { msg := msgHead.(*znet.Message) msg.Data = make([]byte, msg.GetDataLen())
_, err := io.ReadFull(conn, msg.Data) if err != nil { fmt.Println("server unpack data err:", err) return }
fmt.Println("==> Recv Msg: ID=", msg.Id, ", len=", msg.DataLen, ", data=", string(msg.Data)) }
time.Sleep(1*time.Second) } }
|