Introduction

UDP (User Datagram Protocol) is a connectionless transport layer protocol that provides a simple unreliable information delivery service oriented towards transactions. The UDP server is implemented through gudp.Server, while the client is implemented using the gudp.ClientConn object or utility methods.

Usage:

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

API Documentation: https://pkg.go.dev/github.com/gogf/gf/v2/net/gudp

Usage Example

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/net/gudp"
  5. )
  6. func main() {
  7. handler := func(conn *gudp.ServerConn) {
  8. defer conn.Close()
  9. for {
  10. if data, addr, _ := conn.Recv(-1); len(data) > 0 {
  11. fmt.Println(string(data), addr.String())
  12. }
  13. }
  14. }
  15. err := gudp.NewServer("127.0.0.1:8999", handler).Run()
  16. if err != nil {
  17. fmt.Println(err)
  18. }
  19. }

UDPServer runs in a blocking manner. Users can perform concurrent processing in the custom callback function based on the read content.

On Linux, you can use the following command to send UDP data to the server for testing, and then check whether there is output on the server side:

  1. echo "hello" > /dev/udp/127.0.0.1/8999

Documentation

📄️ UDP - ObjectUsing the GoFrame framework for UDP component development, specifically the use of the gudp.Conn connection object. The article provides detailed function interface descriptions and a complete example code for client-server communication, helping developers quickly master the specific operations and application scenarios of the UDP connection object.

📄️ UDP - MethodsCommon utility methods for UDP communication using the gudp module in the GoFrame framework, including how to create a UDP connection with NewNetConn, use Send and SendRecv methods for data transmission, and use *Pkg methods to simplify data packet protocol transmission.