tcp

package
v1.16.0 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2023 License: MIT Imports: 7 Imported by: 0

README

Manual

简单的 tcp server

API

监听并阻塞
func ListenAndServe(ctx context.Context, addr string, fn func(net.Conn, error)) error
向 tcp server 写入一个包
func WritePacket(addr string, packet []byte) (int, error)

示例

package main

import (
	"fmt"
	"io"
	"net"
	"time"

	"golang.org/x/net/context"

	"github.com/o8x/jk/logger"
	"github.com/o8x/jk/signal"
	"github.com/o8x/jk/tcp"
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())

	go func() {
		time.Sleep(time.Second * 5)
		logger.Info("canceled")
		cancel()
	}()

	err := tcp.ListenAndServe(ctx, ":64458", func(conn net.Conn, err error) {
		if err != nil {
			fmt.Println(err)
			return
		}

		all, err := io.ReadAll(conn)
		if err != nil {
			fmt.Println(err)
			return
		}

		fmt.Println(conn.RemoteAddr(), string(all))
		logger.Info("connect released")
	})

	if err != nil {
		panic(err)
	}

	logger.Info("application exit")
}

运行它

> go run .
INFO[0000] tcp server listen on tcp://[::]:64458        
INFO[0005] canceled                                     
INFO[0005] tcp server closed                            
127.0.0.1:65037 Hello

INFO[0011] connect released                             
INFO[0011] application exit  

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListenAndServe

func ListenAndServe(ctx context.Context, addr string, fn func(net.Conn, error)) error

func WritePacket added in v1.7.1

func WritePacket(addr string, packet []byte) (int, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

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