client.go 784 Bytes
package main

import (
	"bytes"
	"encoding/binary"
	"net"
	net2 "pro2d/components/net"
	"pro2d/utils"
)

func main() {

	head := net2.Head{
		Length:   0,
		Cmd:      1,
		ErrCode:  0,
		PreField: 0,
	}


	b := net2.MsgPkg{
		Head: head,
		Body: []byte("hello world"),
	}
	head.Length = int32(16 + len(b.Body))
	buf := &bytes.Buffer{}
	err := binary.Write(buf, binary.BigEndian, head)
	if err != nil {
		utils.Sugar.Errorf("err: %v, head: %v", err, head)
		return
	}
	utils.Sugar.Debugf("head: %v", head)

	err = binary.Write(buf, binary.BigEndian, b.Body)
	if err != nil {
		utils.Sugar.Errorf("err: %v, msg: %v", err, b.Body)
		return
	}

	client, err := net.Dial("tcp", "localhost:8849")
	if err != nil {
		utils.Sugar.Error(err)
		return
	}
	client.Write(buf.Bytes())
	select {}
}