Hi,

I recently discovered this strange behaviour, Windows allows the 
coexistence of three types of sockets on the same transport-layer service 
port, for example, 127.0.0.1:8080, [::1]:8080 and [::ffff:0.0.0.0]:8080.

So if you use something like:

net.Listen("tcp", fmt.Sprintf(":%d", port)

and another program already uses the same port using an IPv4 address, 
Listen will not fail.

If instead you use:

net.Listen("tcp4", fmt.Sprintf(":%d", port)

it will fail as expected.

Based on the following issues, this is the intended behaviour 

https://github.com/golang/go/issues/45150
https://github.com/golang/go/issues/30291

so a Go listener behaves differently based on the operating system and this 
could cause unexpected bug reports from Windows users.

In my program I want to listen on both IPv4 and IPv6 (if available) and so 
I use "tcp" as address.

To be able to detect port conflict with other programs, I have to do 
something like this:

if runtime.GOOS != osWindows {
return
}
listener, err := net.Listen("tcp4", fmt.Sprintf(":%d", port))
if err != nil {
os.Exit(1)
}
listener.Close()

before starting net.Listen with "tcp" address.

So all of you use a similar fix? Do you know a cleaner solution? 

I would have expected such a workaround within Go itself if it is the only 
way.

Based on the above issues, python had the same problem in the past but it 
seems now fixed in recent versions.

Thank you
Nicola

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/cf310370-eaea-45a8-bf13-1a8bbc919fc4n%40googlegroups.com.

Reply via email to