Write to Client UDP Socket in Go

前端 未结 2 546
悲&欢浪女
悲&欢浪女 2021-02-07 06:01

I\'m looking for a good solution for a client/server communication with UDP sockets in Go language.

The examples I found on the Internet show me how to send data to the

相关标签:
2条回答
  • 2021-02-07 06:13

    hello_echo.go

     package main                                                                                                          
    
     import (                                                                                                              
         "bufio"                                                                                                           
         "fmt"                                                                                                             
         "net"                                                                                                             
         "time"                                                                                                            
     )                                                                                                                     
    
     const proto, addr = "udp", ":8888"                                                                                    
    
     func main() {  
    
         go func() {                                                                                                       
             conn, _ := net.ListenPacket(proto, addr)                                                                      
             buf := make([]byte, 1024)                                                                                     
             n, dst, _ := conn.ReadFrom(buf)                                                                               
             fmt.Println("serv recv", string(buf[:n]))                                                                     
             conn.WriteTo(buf, dst)                                                                                        
         }()        
    
         time.Sleep(1 * time.Second)   
    
         conn, _ := net.Dial(proto, addr)                                                                                  
         conn.Write([]byte("hello\n"))                                                                                     
         buf, _, _ := bufio.NewReader(conn).ReadLine()                                                                     
         fmt.Println("clnt recv", string(buf))                                                                             
     }                                                                                                                     
    
    0 讨论(0)
  • 2021-02-07 06:15

    Check the below samples for client/server communication over UDP. The sendResponse routine is for sending response back to client.

    udpclient.go

    package main
    import (
        "fmt"
        "net"
        "bufio"
    )
    
    func main() {
        p :=  make([]byte, 2048)
        conn, err := net.Dial("udp", "127.0.0.1:1234")
        if err != nil {
            fmt.Printf("Some error %v", err)
            return
        }
        fmt.Fprintf(conn, "Hi UDP Server, How are you doing?")
        _, err = bufio.NewReader(conn).Read(p)
        if err == nil {
            fmt.Printf("%s\n", p)
        } else {
            fmt.Printf("Some error %v\n", err)
        }
        conn.Close()
    }
    

    udpserver.go

    package main
    import (
        "fmt" 
        "net"  
    )
    
    
    func sendResponse(conn *net.UDPConn, addr *net.UDPAddr) {
        _,err := conn.WriteToUDP([]byte("From server: Hello I got your message "), addr)
        if err != nil {
            fmt.Printf("Couldn't send response %v", err)
        }
    }
    
    
    func main() {
        p := make([]byte, 2048)
        addr := net.UDPAddr{
            Port: 1234,
            IP: net.ParseIP("127.0.0.1"),
        }
        ser, err := net.ListenUDP("udp", &addr)
        if err != nil {
            fmt.Printf("Some error %v\n", err)
            return
        }
        for {
            _,remoteaddr,err := ser.ReadFromUDP(p)
            fmt.Printf("Read a message from %v %s \n", remoteaddr, p)
            if err !=  nil {
                fmt.Printf("Some error  %v", err)
                continue
            }
            go sendResponse(ser, remoteaddr)
        }
    }
    
    0 讨论(0)
提交回复
热议问题