GoLang TCP
Simple TCP message passing in Go
API
- github.com/evindunn/gtcp/pkg/tcpclient
- Send(addrStr string, msgStr string) error
- github.com/evindunn/gtcp/pkg/tcpserver
- NewServer(port int, handler ConnectionHandler) (*Server, error)
- Server.Start()
- github.com/evindunn/gtcp/pkg/tcpmessage
- NewMessage(content string, isCompressed bool) Message
- MessageFromConnection(c *net.Conn) (*Message, error)
- Message.ToBytes() []byte
- Message.GetContent() []byte
- Message.GetSize() int
- Message.IsCompressed() bool
- Message.Compress() error
- Message.Decompress() error
Examples
Handle connections using the interface defined in ConnectionHandler.go
type Handler struct {}
func (h *Handler) HandleConnection(c *net.Conn) {
defer (*c).Close()
log.Println("Connected!")
}
Then use the handler with the Server struct
var h Handler
srv, err := tcpServer.NewServer(8080, &h)
if err != nil {
fmt.Fprintf(os.Stderr, "An error occurred creating the server: %s\n", err)
os.Exit(1)
}
srv.Start()
The Message class defines a simple protocol for passing messages in TCP
|------- 8 bytes---------|------- 1 byte ---------|------- Remaining bytes ---------|
|----- messageSize ------|----- isCompressed -----|---------- content --------------|