1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| package main
import ( "log" "net" "fmt" )
func main() { hostName := "localhost" portNum := "6000" service := hostName + ":" + portNum RemoteAddr, err := net.ResolveUDPAddr("udp", service) // LocalAddr := nil conn, err := net.DialUDP("udp", nil, RemoteAddr) if err != nil { log.Fatal(err) } log.Printf("Established connection to %s \n", service) log.Printf("Remote UDP address : %s \n", conn.RemoteAddr().String()) log.Printf("Local UDP client address : %s \n", conn.LocalAddr().String()) defer conn.Close() message := []byte("Hello UDP server!") _, err = conn.Write(message) if err != nil { log.Println(err) } buffer := make([]byte, 1024) n, addr, err := conn.ReadFromUDP(buffer) fmt.Println("UDP Server : ", addr) fmt.Println("Received from UDP server : ", string(buffer[:n]))
}
|