FAQ
I'm not sure if this is a bug, or an idiosyncrasy of Linux. Hopefully
someone can shed some light on it.

I'm using Go 1.0.2 on both Windows 7 64-bit and Ubuntu 12.04 64-bit. I'm
writing a simple program to send data to a UDP socket, on a specified
host/port. I took the basic example from Jan Newmarch's excellent book
(http://jan.newmarch.name/go/all.pdf). Here is my modified version of it:

package main
import "net"
func main() {
udpAddr, err := net.ResolveUDPAddr("udp4", "127.0.0.1:4000")
if err != nil { panic(err) }
conn, err := net.DialUDP("udp4", nil, udpAddr)
if err != nil { panic(err) }
_, err = conn.Write([]byte("anything"))
if err != nil { panic(err) }
}

This example works on both my systems (Windows 7 64-bit & Ubuntu 12.04
64-bit). But if I try to send more than one packet, like this:

package main
import "net"
func main() {
udpAddr, err := net.ResolveUDPAddr("udp4", "127.0.0.1:4000")
if err != nil { panic(err) }
conn, err := net.DialUDP("udp4", nil, udpAddr)
if err != nil { panic(err) }
_, err = conn.Write([]byte("anything"))
if err != nil { panic(err) }
_, err = conn.Write([]byte("anything")) // ADDED
if err != nil { panic(err) } // ADDED
}

The second Write() call (and every second Write() call, if I put it in a
loop) fails ("connection refused") under these circumstances:
- Only on Ubuntu, never on Windows.
- Only if the destination address is 127.0.0.1. Any other destination works
fine.
- Only if there is no other app on the Ubuntu box receiving UDP packets on
port 4000.

So it's almost as if Linux (or the Go runtime) checks, when the destination
is localhost, whether anything is waiting to receive the packets that it's
about to send, and fails the Write() call if not (but only every second
Write() call). I've done the same thing with C++ and Python on Linux, and
never seen this behaviour, which makes me think it's specific to Go.

Any help much appreciated.

--

Search Discussions

  • Mikio Hara at Sep 21, 2012 at 2:37 pm

    On Fri, Sep 21, 2012 at 6:42 PM, wrote:

    I'm not sure if this is a bug, or an idiosyncrasy of Linux. Hopefully
    someone can shed some light on it.
    Dial, which calls connect internally. I'm guessing that;
    - your first write writes your message to the 127.0.0.1:4000,
    - protocol stacks within the kernel tries to deliver your
    message but fails (because of there's no receiver) then returns
    ICMP error to the sender,
    - second write reports ECONNREFUSED.

    -- Mikio

    --
  • Bryanturley at Sep 21, 2012 at 7:24 pm
    if that is the case (the icmp error) you could use netcat to listen to your
    incoming packets.
    nc -l -u 4000 i think should work, read the man page that was off the top
    of my head
    also you should get those strings written to your console so you will know
    if the data got through.

    On Friday, September 21, 2012 9:32:16 AM UTC-5, Mikio Hara wrote:

    On Fri, Sep 21, 2012 at 6:42 PM, <rosscan...@gmail.com <javascript:>>
    wrote:
    I'm not sure if this is a bug, or an idiosyncrasy of Linux. Hopefully
    someone can shed some light on it.
    Dial, which calls connect internally. I'm guessing that;
    - your first write writes your message to the 127.0.0.1:4000,
    - protocol stacks within the kernel tries to deliver your
    message but fails (because of there's no receiver) then returns
    ICMP error to the sender,
    - second write reports ECONNREFUSED.

    -- Mikio
    --
  • Rosscanning2010 at Sep 21, 2012 at 10:27 pm

    Thanks for the replies. Mikio's theory was right: Dial() calls connect()
    internally, and that's what's causing this behaviour. I reproduced the
    problem using C++, so this is nothing to do with Go.
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 21, '12 at 1:52p
activeSep 21, '12 at 10:27p
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase