Problem With http.ListenAndServe Use IPV6

2,079 views
Skip to first unread message

smallfish

unread,
Jul 15, 2010, 10:18:39 PM7/15/10
to golang-nuts
Hi, all

   I wrote some http server example, find default server run with tcp6. so visit the ipv6 address with some problem.

   because i disable the ipv6 :(

   also can mody the src/pkg/http/server.go source, change net.Listen("tcp", addr) -> net.Listen("tcp4", addr).

   but it's a not good idea. can add declare a variable with tcp/tcp4/tcp6 etc...

CODE:
// ----------------------------------------------------------
var ServeType = "tcp"
func ListenAndServe(addr string, handler Handler) os.Error {
    l, e := net.Listen(ServeType, addr)
// ----------------------------------------------------------

APP CODE(old code don't need to mody, default is tcp):
// ----------------------------------------------------------
    http.ServeType = "tcp4"
    err := http.ListenAndServe(":8080", nil)
// ----------------------------------------------------------

   thanks all. :)
--
Go. 春暖花开 http://chenxiaoyu.org  twitter: @nnfish

MC.Spring

unread,
Jul 16, 2010, 3:56:39 AM7/16/10
to golang-nuts
According to you, I think it's a little more flexible way for users.

Do you think of adding a pair of new methods for setter/getter of
this?

On Jul 16, 10:18 am, smallfish <smallfish...@gmail.com> wrote:
> Hi, all
>
> I wrote some http server example, find default server run with tcp6. so
> visit the ipv6 address with some problem.
>
> because i disable the ipv6 :(
>
> also can mody the src/pkg/http/server.go source, change net.Listen("tcp",
> addr) -> net.Listen("tcp4", addr).
>
> but it's a not good idea. can add declare a variable with tcp/tcp4/tcp6
> etc...
>
> CODE:
> // ----------------------------------------------------------
> *var ServeType = "tcp"*
> *func ListenAndServe(addr string, handler Handler) os.Error {*
> * l, e := net.Listen(ServeType, addr)*
> // ----------------------------------------------------------
>
> APP CODE(old code don't need to mody, default is tcp):
> // ----------------------------------------------------------
> * http.ServeType = "tcp4"*
> * err := http.ListenAndServe(":8080", nil)*

smallfish

unread,
Jul 16, 2010, 4:12:40 AM7/16/10
to MC.Spring, golang-nuts
xd, setter/getter .... That's Java...

--
Go. 春暖花开 http://chenxiaoyu.org  twitter: @nnfish



Russ Cox

unread,
Jul 16, 2010, 1:56:16 PM7/16/10
to smallfish, golang-nuts
"tcp" is the right default. It means both ipv6 and ipv4.
Unfortunately it doesn't quite work on some Linux systems
that have decided, inexplicably, that by default you shouldn't
be able to have one socket do both. This is
http://code.google.com/p/go/issues/detail?id=679

http.ListenAndServe is just a convenience wrapper.
It doesn't need options.
If you need to listen on a different network, call
net.Listen with the appropriate parameters and then
call http.Serve on the resulting listener.

Russ

smallfish

unread,
Jul 16, 2010, 9:41:55 PM7/16/10
to r...@golang.org, golang-nuts
Thanks Russ, I see.
I think if has the options, use ListenAndServe() will better.
:_)

--
Go. 春暖花开 http://chenxiaoyu.org  twitter: @nnfish



2010/7/17 Russ Cox <r...@golang.org>
Reply all
Reply to author
Forward
0 new messages