xgdapg / xconn

Convert TCP stream to packets

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Usage

package main

import (
	"fmt"
	"github.com/xgdapg/xconn"
	"net"
	"time"
)

func main() {
	conn, err := net.Dial("tcp", "127.0.0.1:8000")
	if err != nil {
		return
	}
	c := xconn.NewConn(conn)
	h := &Handler{conn: c}
	c.SetMsgHandler(h)
	c.SetCloseHandler(h)
	for i := 0; i < 5; i++ {
		time.Sleep(time.Second)
		c.SendData([]byte("hello!"))
	}
	time.Sleep(10 * time.Second)
}

type Handler struct {
	conn *xconn.Conn
}

func (this *Handler) HandleMsg(msg *xconn.MsgData) {
	fmt.Println("RECV: " + string(msg.Data))
}

func (this *Handler) HandleClose() {
	fmt.Println("CLOSED")
}

About

Convert TCP stream to packets


Languages

Language:Go 100.0%