Skip to content

Latest commit

 

History

History
75 lines (59 loc) · 1.44 KB

README.md

File metadata and controls

75 lines (59 loc) · 1.44 KB

🪡bttcp

Binary Interaction Protocol Based on TCP

golang

introduction

bttcp is a binary transmission protocol based on TCP, where the response header only records the length of the binary stream and data exchange is limited to byte exchange. bttcp is designed to improve transmission performance.

installing

Select the version to install

go get github.com/go-needle/bttcp@version

If you have already get , you may need to update to the latest version

go get -u github.com/go-needle/bttcp

quickly start

server code

package main

import "github.com/go-needle/bttcp"

func main() {
	s := bttcp.NewServer(bttcp.HandlerFunc(func(b []byte) []byte{
		return b
	}))
	s.Run(9999)
}

client code

package main

import (
	"fmt"
	"github.com/go-needle/bttcp"
	"math/rand"
	"strconv"
	"sync"
	"time"
)
var wg sync.WaitGroup

func main() {
	c := bttcp.NewClient("127.0.0.1:9999", 100, true)
	for i := 0; i < 10000; i++ {
		wg.Add(1)
		num := i
		go func() {
			time.Sleep(time.Duration(rand.Intn(100)) * time.Second)
			resp, err := c.Send([]byte("hello" + strconv.Itoa(num)))
			if err != nil {
				return
			}
			fmt.Println(string(resp))
			wg.Done()
		}()
	}
	wg.Wait()
	c.Close()
}