W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
?gudp
?模塊提供了非常簡便易用的?gudp.Conn
?鏈接操作對象。
使用方式:
import "github.com/gogf/gf/v2/net/gudp"
接口文檔: https://pkg.go.dev/github.com/gogf/gf/v2/net/gudp
type Conn
func NewConn(raddr string, laddr ...string) (*Conn, error)
func NewConnByNetConn(udp *net.UDPConn) *Conn
func (c *Conn) Close() error
func (c *Conn) LocalAddr() net.Addr
func (c *Conn) Recv(length int, retry ...Retry) ([]byte, error)
func (c *Conn) RecvPkg(retry ...Retry) (result []byte, err error)
func (c *Conn) RecvPkgWithTimeout(timeout time.Duration, retry ...Retry) ([]byte, error)
func (c *Conn) RecvWithTimeout(length int, timeout time.Duration, retry ...Retry) ([]byte, error)
func (c *Conn) RemoteAddr() net.Addr
func (c *Conn) Send(data []byte, retry ...Retry) error
func (c *Conn) SendPkg(data []byte, retry ...Retry) error
func (c *Conn) SendPkgWithTimeout(data []byte, timeout time.Duration, retry ...Retry) error
func (c *Conn) SendRecv(data []byte, receive int, retry ...Retry) ([]byte, error)
func (c *Conn) SendRecvPkg(data []byte, retry ...Retry) ([]byte, error)
func (c *Conn) SendRecvPkgWithTimeout(data []byte, timeout time.Duration, retry ...Retry) ([]byte, error)
func (c *Conn) SendRecvWithTimeout(data []byte, receive int, timeout time.Duration, retry ...Retry) ([]byte, error)
func (c *Conn) SendWithTimeout(data []byte, timeout time.Duration, retry ...Retry) error
func (c *Conn) SetDeadline(t time.Time) error
func (c *Conn) SetRecvBufferWait(d time.Duration)
func (c *Conn) SetRecvDeadline(t time.Time) error
func (c *Conn) SetSendDeadline(t time.Time) error
可以看到,?gudp.Conn
?和?gtcp.Conn
?的方法非常類似,并且也支持簡單協(xié)議的消息包方法。
?gudp.Conn
?的操作絕大部分類似于?gtcp
?的操作方式(大部分的方法名稱也相同),但由于?UDP
?是面向非連接的協(xié)議,因此?gudp.Conn
?(底層通信端口)也只能完成最多一次數(shù)據(jù)寫入和讀取,客戶端下一次再與目標服務端進行通信的時候,將需要創(chuàng)建新的?Conn
?對象進行通信。
package main
import (
"fmt"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/gudp"
"github.com/gogf/gf/v2/os/gtime"
"time"
)
func main() {
// Server
go gudp.NewServer("127.0.0.1:8999", func(conn *gudp.Conn) {
defer conn.Close()
for {
data, err := conn.Recv(-1)
if len(data) > 0 {
if err := conn.Send(append([]byte("> "), data...)); err != nil {
g.Log().Error(err)
}
}
if err != nil {
g.Log().Error(err)
}
}
}).Run()
time.Sleep(time.Second)
// Client
for {
if conn, err := gudp.NewConn("127.0.0.1:8999"); err == nil {
if b, err := conn.SendRecv([]byte(gtime.Datetime()), -1); err == nil {
fmt.Println(string(b), conn.LocalAddr(), conn.RemoteAddr())
} else {
g.Log().Error(err)
}
conn.Close()
} else {
g.Log().Error(err)
}
time.Sleep(time.Second)
}
}
該示例與?gtcp.Conn
?中的通信示例類似,不同的是,客戶端與服務端無法保持連接,每次通信都需要創(chuàng)建的新的連接對象進行通信。
執(zhí)行后,輸出結(jié)果如下:
> 2018-07-21 23:13:31 127.0.0.1:33271 127.0.0.1:8999
> 2018-07-21 23:13:32 127.0.0.1:45826 127.0.0.1:8999
> 2018-07-21 23:13:33 127.0.0.1:58027 127.0.0.1:8999
> 2018-07-21 23:13:34 127.0.0.1:33056 127.0.0.1:8999
> 2018-07-21 23:13:35 127.0.0.1:39260 127.0.0.1:8999
> 2018-07-21 23:13:36 127.0.0.1:33967 127.0.0.1:8999
> 2018-07-21 23:13:37 127.0.0.1:52359 127.0.0.1:8999
...
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: