Can't read UDP reply (golang)

后端 未结 1 1725
失恋的感觉
失恋的感觉 2020-12-19 17:34

I\'m working on a Go program that sends out a UDP broadcast to query existence of devices on the local network and then reads the replies. Using Wireshark I confirm that the

相关标签:
1条回答
  • 2020-12-19 18:09

    You need to use ListenUDP instead of DialUDP. When you use DialUDP, it creates a "connected" UDP port, and only packets originating from the remote address are returned on read.

    conn, err = net.ListenUDP("udp", ourAddr)
    

    Since the connection doesn't have a default destination, you will also need to use WriteTo* methods to send packets:

    sendLen, err := conn.WriteToUDP(discoverMsg, serverAddr)
    
    0 讨论(0)
提交回复
热议问题