net.http ListenAndServe. netstat only list ipv6 port

7,587 views
Skip to first unread message

che...@garena.com

unread,
Jul 8, 2013, 4:31:40 AM7/8/13
to golan...@googlegroups.com
Ubuntu 10.04
Go 1.1.1
In the code:
    Log.Fatal(http.ListenAndServe(":3000", nil)
Then 'netstat -a | grep LISTEN', we can only see
    tcp6 0 0 [::]:3000     [::]:* LISTEN

There's no 'tcp 0 0 *:3000 *:* LISTEN', but actually the server is able to be visited with IPv4 addresses. 

Even when I changed the code to
   Log.Fatal(http.ListenAndServe("0.0.0.0:3000", nil)
The issue still keeps the same.

Anyone know the reason and solution? Thanks a lot.

Svip

unread,
Jul 8, 2013, 5:03:24 AM7/8/13
to che...@garena.com, golan...@googlegroups.com
If the server is visitable via IPv4, why is this a problem? Or are
you looking for an answer?

If so, I think it has something to do with IPv6 support in Linux
systems, which prefers IPv6 and then later reverts to IPv4 if IPv6 is
not available. I could be wrong, but I assume that's what's
happening.

minux

unread,
Jul 8, 2013, 6:37:10 AM7/8/13
to che...@garena.com, golan...@googlegroups.com
on linux, by default, net.ipv6.bindv6only is 0, so ipv4 packets could also be received from
ipv6 sockets with ipv4-mapped ipv6 address. thus you only need to listen on tcp6 socket
and we can support both ipv4 and ipv6.

if you want explicitly only listen on ipv4 port, you will have to use net.Listen("tcp4", "0.0.0.0:3000")
and then pass the listener to http.Serve.

che...@garena.com

unread,
Jul 8, 2013, 10:39:02 PM7/8/13
to golan...@googlegroups.com
Thanks for both of your answers.

Yes, there's no problem regards to usage. The only problem is that it confuses some administration tools and persons. Since normally no one are using IPv6 addresses, so they only check the ipv4 ports and cannot find the proper result.

The question is that other programs (such as sshd) listen both at IPv4 and IPv6 ports and I thought that was a normal behaviour compared with net/http. The net/http's behavior is not correct in my opinion, especially when I explicitly wrote "ListenAndServ("0.0.0.0:80", nil)" .   

Dave Cheney

unread,
Jul 8, 2013, 10:50:13 PM7/8/13
to che...@garena.com, golang-nuts
ListenAndServer is a convenience, if you need more control then use
net.ListenTCP("tcp4", ...) then pass that listener to net.Serve.
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
Reply all
Reply to author
Forward
0 new messages