How to net.Listen to both tcp4 and tcp6?

2,768 views
Skip to first unread message

Constantine Vassilev

unread,
Oct 5, 2015, 7:52:01 PM10/5/15
to golang-nuts
There is an issue in net.Listen which I don't see how to solve.
I want to listen to both tcp4 and tcp6.
If I use "tcp", Golang defaults to ipv4 internally.
If I specify tcp4 the server is serving only ipv4 requests.
If I specify tcp6 the server is serving only ipv6 requests.

I cannot use go routines with srv.Serve - it is not working.
The only way I see this to work is to have two separate 
Golang apps running in parallel - one listening to ipv4, the other - to ipv6.
This is very inconvenient and hard to maintain.

Any ideas?

==============================================================
srv := &http.Server{}
//
ln, err1 := net.Listen("tcp", addr+":443")
if err1 != nil {
fmt.Printf("net.Listen(tcp): %s", err1)
}
tlsListener := tls.NewListener(tcpKeepAliveListener{ln.(*net.TCPListener)}, config)
srv.Serve(tlsListener)
//
==============================================================

==============================================================
srv := &http.Server{}
//
ln4, err1 := net.Listen("tcp4", addr+":443")
if err1 != nil {
fmt.Printf("net.Listen(tcp4): %s", err1)
}
tlsListener4 := tls.NewListener(tcpKeepAliveListener{ln4.(*net.TCPListener)}, config)
srv.Serve(tlsListener4)
//
==============================================================

==============================================================
srv := &http.Server{}
//
ln6, err1 := net.Listen("tcp6", addr+":443")
if err1 != nil {
fmt.Printf("net.Listen(tcp6): %s", err1)
}
tlsListener6 := tls.NewListener(tcpKeepAliveListener{ln6.(*net.TCPListener)}, config)
srv.Serve(tlsListener6)
//
==============================================================

dja...@gmail.com

unread,
Oct 6, 2015, 6:21:59 AM10/6/15
to golang-nuts


On Tuesday, October 6, 2015 at 2:52:01 AM UTC+3, Constantine Vassilev wrote:
There is an issue in net.Listen which I don't see how to solve.
I want to listen to both tcp4 and tcp6.
If I use "tcp", Golang defaults to ipv4 internally.

it should listen to  both, but see

If I specify tcp4 the server is serving only ipv4 requests.
If I specify tcp6 the server is serving only ipv6 requests.

I cannot use go routines with srv.Serve - it is not working.

why not working ? just add 'go' to first  srv.Serve:

go srv.Serve(tlsListener4)
srv.Serve(tlsListener6)
Поздрави,
Djadala

James Bardin

unread,
Oct 6, 2015, 9:11:32 AM10/6/15
to golang-nuts, dja...@gmail.com


On Tuesday, October 6, 2015 at 6:21:59 AM UTC-4, dja...@gmail.com wrote:
it should listen to  both, but see


That isn't really relevant at all to running the http.Server

 
If I specify tcp4 the server is serving only ipv4 requests.
If I specify tcp6 the server is serving only ipv6 requests.

I cannot use go routines with srv.Serve - it is not working.

why not working ? just add 'go' to first  srv.Serve:

go srv.Serve(tlsListener4)
srv.Serve(tlsListener6)



Or you could preserve the errors from Serve by returning them over a channel, as was demonstrated in the first thread about this topic.  

dja...@gmail.com

unread,
Oct 6, 2015, 11:12:31 AM10/6/15
to golang-nuts, dja...@gmail.com


On Tuesday, October 6, 2015 at 4:11:32 PM UTC+3, James Bardin wrote:


On Tuesday, October 6, 2015 at 6:21:59 AM UTC-4, dja...@gmail.com wrote:
it should listen to  both, but see


That isn't really relevant at all to running the http.Server


Hi,
how it isn't relevant, if OP call net.Listen && bug states:
On DragonFly BSD and OpenBSD, listening on the "tcp" and "udp" networks does not listen for both IPv4 and IPv6 connections.  ?
Regards,
Djadala

James Bardin

unread,
Oct 6, 2015, 11:22:00 AM10/6/15
to dja...@gmail.com, golang-nuts
On Tue, Oct 6, 2015 at 11:12 AM, <dja...@gmail.com> wrote:
> Hi,
> how it isn't relevant, if OP call net.Listen && bug states:
> On DragonFly BSD and OpenBSD, listening on the "tcp" and "udp" networks does
> not listen for both IPv4 and IPv6 connections. ?

Sorry, I completely missed the second "bug" in that block and thought
you were referring to how ReadFrom works on POSIX.

Since it's not a Go issue per say, and the example all had 2
listeners, I don't think there should be any confusion around how some
BSDs listen on dual stack ;)
Reply all
Reply to author
Forward
0 new messages