udp

package
v1.9.1 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2023 License: MIT Imports: 7 Imported by: 0

README

Manual

简单的 udp server

API

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

示例

package main

import (
	"context"
	"fmt"
	"net"
	"time"

	"github.com/o8x/jk/logger"
	"github.com/o8x/jk/udp"
)

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

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

	err := udp.ListenAndServe(ctx, ":64458", func(bytes []byte, addr net.Addr, err error) {
		if err != nil {
			fmt.Println(err)
			return
		}

		fmt.Println(addr.String(), string(bytes))
		logger.Info("released")
	})

	if err != nil {
		panic(err)
	}

	logger.Info("application exit")
}

运行它

> go run .
INFO[0000] udp server listen on udp://[::]:64458        
127.0.0.1:53966 Hello

INFO[0002] released
INFO[0005] canceled
INFO[0005] udp server closed
INFO[0005] 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([]byte, net.Addr, 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