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: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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
⌂ 换网址