示例#1
0
文件: client.go 项目: danuxguin/dxnet
func main() {

	conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", config.SERVER_IP, config.SERVER_PORT))
	if err != nil {
		logger.Errorf("net.Dial err=%v server_ip_port = %v", err, fmt.Sprintf("%s:%s", config.SERVER_IP, config.SERVER_PORT))
		return
	}

	for {
		data := make([]byte, 1024)

		pos := 0
		common.WriteUint32(data[pos:pos+4], uint32(1001))
		pos += 4
		common.WriteUint32(data[pos:pos+4], uint32(4))
		pos += 4
		common.WriteUint32(data[pos:pos+4], uint32(4))
		pos += 4
		common.WriteUint32(data[pos:pos+4], uint32(4))
		pos += 4
		common.WriteUint32(data[pos:pos+4], uint32(1))
		pos += 4

		_, err := conn.Write(data[0:pos])
		if err != nil {
			fmt.Printf("net.Write err=%v\n", err)
			return
		}

		c, err := io.ReadAtLeast(conn, data, 16)
		if err != nil {
			fmt.Printf("err=%v\n", err)
			return
		}

		fmt.Printf("header=%v\n", data[0:c])

		// header := network.ParseHeader(data[0:common.PACKET_HEAD_LEN])
		//
		// _, err = conn.Read(data[0:header.MsgLen])
		// if err != nil {
		// 	fmt.Printf("err=%v\n", err)
		// 	return
		// }
		result, err := ioutil.ReadAll(conn)
		if err != nil {
			fmt.Printf("err=%v\n", err)
			return
		}
		fmt.Printf("result=%v\n", string(result))

		// fmt.Printf("header=%#v\n", header)
		// fmt.Printf("data=%v len=%v\n", data[0:c], c)

		conn.Close()
	}
}
示例#2
0
//send data to session
func (this *Session) SendMSG(msg *Message) error {
	//加密
	//压缩
	Data := make([]byte, MAX_PACKAGE_LEN)

	pos := 0
	common.WriteUint32(Data[pos:pos+4], msg.MsgHeader.MsgID)
	pos += 4
	common.WriteUint32(Data[pos:pos+4], msg.MsgHeader.MsgVer)
	pos += 4
	common.WriteUint32(Data[pos:pos+4], msg.MsgHeader.MsgLen)
	pos += 4
	common.WriteUint32(Data[pos:pos+4], msg.MsgHeader.MsgCpsLen)
	pos += 4
	common.WriteUint32(Data[pos:pos+4], uint32(100))
	pos += 4

	_, err := this.conn.Write(Data[0:20])
	return err
}