gudp模块提供了非常简便易用的gudp.Conn链接操作对象。

使用方式

  1. import "github.com/gogf/gf/g/net/gudp"

接口文档https://godoc.org/github.com/gogf/gf/g/net/gudp

  1. type Conn
  2. func NewConn(raddr string, laddr ...string) (*Conn, error)
  3. func NewConnByNetConn(udp *net.UDPConn) *Conn
  4. func (c *Conn) Close() error
  5. func (c *Conn) LocalAddr() net.Addr
  6. func (c *Conn) Recv(length int, retry ...Retry) ([]byte, error)
  7. func (c *Conn) RecvPkg(retry ...Retry) (result []byte, err error)
  8. func (c *Conn) RecvPkgWithTimeout(timeout time.Duration, retry ...Retry) ([]byte, error)
  9. func (c *Conn) RecvWithTimeout(length int, timeout time.Duration, retry ...Retry) ([]byte, error)
  10. func (c *Conn) RemoteAddr() net.Addr
  11. func (c *Conn) Send(data []byte, retry ...Retry) error
  12. func (c *Conn) SendPkg(data []byte, retry ...Retry) error
  13. func (c *Conn) SendPkgWithTimeout(data []byte, timeout time.Duration, retry ...Retry) error
  14. func (c *Conn) SendRecv(data []byte, receive int, retry ...Retry) ([]byte, error)
  15. func (c *Conn) SendRecvPkg(data []byte, retry ...Retry) ([]byte, error)
  16. func (c *Conn) SendRecvPkgWithTimeout(data []byte, timeout time.Duration, retry ...Retry) ([]byte, error)
  17. func (c *Conn) SendRecvWithTimeout(data []byte, receive int, timeout time.Duration, retry ...Retry) ([]byte, error)
  18. func (c *Conn) SendWithTimeout(data []byte, timeout time.Duration, retry ...Retry) error
  19. func (c *Conn) SetDeadline(t time.Time) error
  20. func (c *Conn) SetRecvBufferWait(d time.Duration)
  21. func (c *Conn) SetRecvDeadline(t time.Time) error
  22. func (c *Conn) SetSendDeadline(t time.Time) error

可以看到,gudp.Conngtcp.Conn的方法非常类似,并且也支持简单协议的消息包方法。

基本介绍

gudp.Conn的操作绝大部分类似于gtcp的操作方式(大部分的方法名称也相同),但由于UDP是面向非连接的协议,因此gudp.Conn(底层通信端口)也只能完成最多一次数据写入和读取,客户端下一次再与目标服务端进行通信的时候,将需要创建新的连接进行通信。

使用示例

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/gogf/gf/g/os/glog"
  6. "github.com/gogf/gf/g/os/gtime"
  7. "github.com/gogf/gf/g/net/gudp"
  8. )
  9. func main() {
  10. // Server
  11. go gudp.NewServer("127.0.0.1:8999", func(conn *gudp.Conn) {
  12. defer conn.Close()
  13. for {
  14. data, err := conn.Recv(-1)
  15. if len(data) > 0 {
  16. if err := conn.Send(append([]byte("> "), data...)); err != nil {
  17. glog.Error(err)
  18. }
  19. }
  20. if err != nil {
  21. glog.Error(err)
  22. }
  23. }
  24. }).Run()
  25. time.Sleep(time.Second)
  26. // Client
  27. for {
  28. if conn, err := gudp.NewConn("127.0.0.1:8999"); err == nil {
  29. if b, err := conn.SendRecv([]byte(gtime.Datetime()), -1); err == nil {
  30. fmt.Println(string(b), conn.LocalAddr(), conn.RemoteAddr())
  31. } else {
  32. glog.Error(err)
  33. }
  34. conn.Close()
  35. } else {
  36. glog.Error(err)
  37. }
  38. time.Sleep(time.Second)
  39. }
  40. }

该示例与gtcp.Conn中的通信示例类似,不同的是,客户端与服务端无法保持连接,每次通信都需要创建的新的连接对象进行通信。

执行后,输出结果如下:

  1. > 2018-07-21 23:13:31 127.0.0.1:33271 127.0.0.1:8999
  2. > 2018-07-21 23:13:32 127.0.0.1:45826 127.0.0.1:8999
  3. > 2018-07-21 23:13:33 127.0.0.1:58027 127.0.0.1:8999
  4. > 2018-07-21 23:13:34 127.0.0.1:33056 127.0.0.1:8999
  5. > 2018-07-21 23:13:35 127.0.0.1:39260 127.0.0.1:8999
  6. > 2018-07-21 23:13:36 127.0.0.1:33967 127.0.0.1:8999
  7. > 2018-07-21 23:13:37 127.0.0.1:52359 127.0.0.1:8999
  8. ...