packetize

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 15, 2024 License: MIT Imports: 7 Imported by: 0

README

packetize

A simple library for creating packetized TCP server using Go.

Installation

go get github.com/GreenWix/packetize

Packet Format

Packet is a byte array with maximum length of 65,535 bytes. Packets are sent in this format:

<packet length : uint16> <packet data : bytes>

Example

package main

import (
	"fmt"
	pkize "github.com/GreenWix/packetize"
	"net"
)

func main() {
	listener, _ := pkize.Listen("tcp4", "localhost:1337") // open listening socket
	for {
		conn, err := listener.Accept() // accept TCP connection from client and create a new socket
		if err != nil {
			continue
		}
		go handleClient(conn) // handle client connection in a separate goroutine
	}
}

func handleClient(conn net.Conn) {
	defer conn.Close() // close socket after done

	buf := make([]byte, 32) // buffer for reading data from client
	for {
		conn.Write([]byte("Hello world!")) // write to the client

		readLen, err := conn.Read(buf) // reading from the socket
		if err != nil {
			fmt.Println("error occured during client handling: ", err)
			break
		}

		fmt.Println(string(buf[:readLen]))
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

func (*Conn) Close

func (conn *Conn) Close() error

func (*Conn) LocalAddr

func (conn *Conn) LocalAddr() net.Addr

func (*Conn) Read

func (conn *Conn) Read(b []byte) (n int, err error)

func (*Conn) RemoteAddr

func (conn *Conn) RemoteAddr() net.Addr

func (*Conn) SetDeadline

func (conn *Conn) SetDeadline(t time.Time) error

func (*Conn) SetReadDeadline

func (conn *Conn) SetReadDeadline(t time.Time) error

func (*Conn) SetWriteDeadline

func (conn *Conn) SetWriteDeadline(time.Time) error

func (*Conn) Write

func (conn *Conn) Write(b []byte) (n int, err error)

type Listener

type Listener struct {
	// contains filtered or unexported fields
}

func Listen

func Listen(network string, addr string) (*Listener, error)

func (*Listener) Accept

func (lst *Listener) Accept() (net.Conn, error)

func (*Listener) Addr

func (lst *Listener) Addr() net.Addr

func (*Listener) Close

func (lst *Listener) Close() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL