Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http: No way to bind a server to a tcp4 address. #5197

Closed
gopherbot opened this issue Apr 3, 2013 · 3 comments
Closed

net/http: No way to bind a server to a tcp4 address. #5197

gopherbot opened this issue Apr 3, 2013 · 3 comments

Comments

@gopherbot
Copy link

by sam.falvo:

I've searched for a solution to this issue, but I couldn't find anything obviously
relevant.

What steps will reproduce the problem?
1.  Please see code at http://play.golang.org/p/KyVr28Tw-K
2.  Build go 1.0.3 from source with CGO_ENABLED=0
3.  go build the program referenced in (1) above.
4.  go install it, then copy to a CentOS 5.8 machine.
5.  Execute it with bin/whatever.
6.  In another console, type "netstat -nlp" to view listening sockets.
7.  Observe that the program only waits on :::80, but not on 0.0.0.0:80 or on *:80.
8.  Execute curl -X GET "http://ipv4.address.of.server:8080/";
9.  Observe no connection is possible.
10.  Execute curl -X GET "http://ipv6.address.of.server:8080/";
11.  Observe connection works as expected.

What is the expected output?
I don't have any problems with net/http binding to one network protocol at a time; I can
always launch multiple goroutines for each supported network.  However, the code linked
above is pretty clearly configured to bind to an IPv4 endpoint, not to an IPv6 endpoint
(observe lack of [::]:8080 in the address string).

What do you see instead?
It binds to IPv6 endpoint no matter what, which causes connections to be rejected by my
cloud server, wherein I cannot turn off IPv6 support (I tried everything I knew how, and
then some).

Which compiler are you using (5g, 6g, 8g, gccgo)?
6g


Which operating system are you using?
On Ubuntu 12, it behaves well, as the kernel apparently treats a binding to :::8080 the
same as *:8080.  On CentOS 5.8 (see additional info below), I fail to connect when using
IPv4 addresses.

Which version are you using?  (run 'go version')
go1.0.3

Please provide any additional information below.
Yes, I know CentOS 5.8 isn't supported, but that's what my cloud server is running;
upgrading isn't possible, as the purpose of the code is to perform tests of another
program on an OS configuration that our customers continue using.  Since I have 13
different OS configs to check on, I figured using Go would be ideal since I can just
splat the static binary to all 13 machines at once and be off and running.

I think the long-term solution for this would be to somehow extend the Server struct
with a set of optional fields to help control which network the server should listen on.
 E.g., the following would be ideal:

s := &http.Server{
  Addr: ":8080",
  AddrType: net.TCP4,
  // etc...
}
@adg
Copy link
Contributor

adg commented Apr 3, 2013

Comment 1:

You need to create a net.Listener separately. The http package's ListenAndServe
functions are for convenience, and are not a complete solution.
http://play.golang.org/p/EOZkK1UUpe

@adg
Copy link
Contributor

adg commented Apr 3, 2013

Comment 2:

Status changed to WorkingAsIntended.

@gopherbot
Copy link
Author

Comment 3 by sam.falvo:

OOH!  For all the staring at the docs, I missed the obvious!  Thank you!

This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants