FAQ
Hi

I've just started with Go and have crashed out on the first thing I wanted
to do. I am trying to connect to a piece of hardware that responds to a
broadcast message.

socket, err := net.DialUDP("udp4", nil,
&net.UDPAddr{
IP: net.IPv4bcast,
Port: 1024,
})
n, err := socket.Write(msg)
n, addr, err := socket.ReadFromUDP(resp)

In essence I'm using just these 3 functions.

Wireshark shows me:
42 3.471817 192.168.1.69 255.255.255.255 UDP Source port:
54124 Destination port: 1024
43 3.473027 192.168.1.71 192.168.1.69 UDP Source port: 1024
Destination port: 54124

The response is clearly being sent but ReadFromUDP() does not see it and
after the 5 second timeout reports:
Read returned 0, <nil>, WSARecvFrom udp4 192.168.1.69:59026: resource
temporarily unavailable

Any ideas? I can't find anything helpful on the web except several reports
of similar things.

Thanks
Bob

--

Search Discussions

  • Brainman at Nov 18, 2012 at 11:04 pm

    On Monday, 19 November 2012 06:25:56 UTC+11, bobc wrote:

    In essence I'm using just these 3 functions.
    Please, show us your full program. I assume you are not ignoring error
    messages.

    Alex

    --
  • Mikio Hara at Nov 19, 2012 at 10:16 am
    What if s/DialUDP/ListenUDP/ ?

    DialUDP calls syscall.Connect internally.
    Please take a look at on-line manual of connect syscall.

    --
  • Bob Cowdery at Nov 19, 2012 at 10:32 am

    On 19/11/2012 10:16, Mikio Hara wrote:
    What if s/DialUDP/ListenUDP/ ?

    DialUDP calls syscall.Connect internally.
    Please take a look at on-line manual of connect syscall.
    I did go round the houses a bit and tried other methods including
    DialUDP/ListenUDP but then I got another error saying that I couldn't
    open the socket twice or something to that effect (not on the right
    machine to try this again at the moment). I could drop down the syscall
    level and I have looked at those calls but I want this to be cross
    platform and that seems to defeat the object. I could not find any
    working code examples of a simple UDP broadcast/read.

    --
  • Mikio Hara at Nov 19, 2012 at 11:02 am
    http://play.golang.org/p/iex-UZNBMm
    http://play.golang.org/p/1bZn1a0pAW
    On Mon, Nov 19, 2012 at 7:30 PM, Bob Cowdery wrote:

    I did go round the houses a bit and tried other methods including
    DialUDP/ListenUDP but then I got another error saying that I couldn't open
    the socket twice or something to that effect (not on the right machine to
    try this again at the moment). I could drop down the syscall level and I
    have looked at those calls but I want this to be cross platform and that
    seems to defeat the object. I could not find any working code examples of a
    simple UDP broadcast/read.
    --
  • Bob Cowdery at Nov 19, 2012 at 6:53 pm
    Thanks, that does work. I'm finding the number of network functions,
    their naming and the ways they can be combined a bit confusing. There
    seems to be multiple ways to achieve the same end but only some
    combinations work in given circumstances.

    If I read this right net.ListenPacket("udp", ":0") is a UDP socket open
    and bind to all interfaces, any port.
    Then the address can be made in a variety of ways and then straight
    read/write to the socket.

    Makes sense but the name ListenPacket would have thrown me.
    On 19/11/2012 11:02, Mikio Hara wrote:
    http://play.golang.org/p/iex-UZNBMm
    http://play.golang.org/p/1bZn1a0pAW
    On Mon, Nov 19, 2012 at 7:30 PM, Bob Cowdery wrote:

    I did go round the houses a bit and tried other methods including
    DialUDP/ListenUDP but then I got another error saying that I couldn't open
    the socket twice or something to that effect (not on the right machine to
    try this again at the moment). I could drop down the syscall level and I
    have looked at those calls but I want this to be cross platform and that
    seems to defeat the object. I could not find any working code examples of a
    simple UDP broadcast/read.
    --
  • Mikio Hara at Nov 20, 2012 at 2:33 am

    On Tue, Nov 20, 2012 at 3:53 AM, Bob Cowdery wrote:

    Thanks, that does work.
    Glad to hear.
    Makes sense but the name ListenPacket would have thrown me.
    I guess it comes from Plan 9: http://swtch.com/plan9port/man/man3/dial.html.
    Looks like the net package API is a compromise btw POSIX and Plan 9, and
    works for the people of both worlds.

    --
  • Bryanturley at Nov 20, 2012 at 4:25 am

    I guess it comes from Plan 9:
    http://swtch.com/plan9port/man/man3/dial.html.
    Looks like the net package API is a compromise btw POSIX and Plan 9, and
    works for the people of both worlds.
    Did not know that, it is much cleaner than I am used to.

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 18, '12 at 11:00p
activeNov 20, '12 at 4:25a
posts8
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase