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: same tcp port listened without error on windows #30291

Closed
chengxuncc opened this issue Feb 18, 2019 · 3 comments

Comments

Projects
None yet
3 participants
@chengxuncc
Copy link

commented Feb 18, 2019

What version of Go are you using (go version)?

$ go version
go version go1.11.5 windows/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\cheng\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\cheng\go
set GOPROXY=
set GORACE=
set GOROOT=D:\Programs\Go
set GOTMPDIR=
set GOTOOLDIR=D:\Programs\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\cheng\AppData\Local\Temp\go-build360010358=/tmp/go-build -gno-record-gcc-switches

What did you do?

main.go
package main

import (
	"fmt"
	"log"
	"net"
)

func main()  {
	lister,e:=net.Listen("tcp","0.0.0.0:1080")
	if e!=nil{
		log.Panicln(e)
	}
	fmt.Println("Listen on 0.0.0.0:1080")
	for{
		conn,e:=lister.Accept()
		if e!=nil{
			log.Panicln("Accept Error",e)
		}
		go test_handle(conn)
	}
}
func test_handle(conn net.Conn){
	buf:=make([]byte,1024)
	for{
		n,e:=conn.Read(buf)
		if e!=nil{
			log.Println(e)
			return
		}
		fmt.Println(string(buf[:n]))
	}
}
main.py
import socket
server_address = ('0.0.0.0', 1080)
sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print('starting up on %s port %s' % server_address) 
sock.bind(server_address)
sock.listen(1)
while True:
    connection, client_address = sock.accept()
    try:
        data = connection.recv(1024)
        if data:
            print(client_address,data)
    finally:
        connection.close()

What did you expect to see?

Golang program should raise a port-had-been-used Error.

What did you see instead?

There are three conditions and which program listen on port first:

1. Golang Program vs Golang Program:

Second Golang Program raise a Error

2. Golang Program vs Python Program:

Golang first, Python second -> Both NO Error
Python first, Golang second -> Both NO Error

3. Golang Program vs C# or others Program:

Golang first, C# second -> C# raise a Error
C# first, Golang second -> Both NO Error

@chengxuncc

This comment has been minimized.

Copy link
Author

commented Feb 18, 2019

@mvdan mvdan changed the title Same tcp port listened without error on windows net: same tcp port listened without error on windows Feb 18, 2019

@mikioh

This comment has been minimized.

Copy link
Contributor

commented Feb 20, 2019

I think you perhaps might need to be informed that:

  • the connection setup function Listen does a bit more than the pair of socket and bind system calls, it is able to return a dual-stack passive-open socket, so you have to use "tcp4" instead of "tcp" if you need an IPv4-only passive-open socket; see https://godoc.org/net#Listen,
  • IIRC, unlike Unix variants, Windows has a funny characteristic that the kernel 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.

Also, the story of someone's adventure on the bind system call might be helpful: https://stackoverflow.com/questions/51071020/golang-net-listen-binds-to-port-thats-already-in-use

Can you please re-visit your test programs and circumstances with the point of view mentioned above?

@chengxuncc

This comment has been minimized.

Copy link
Author

commented Feb 20, 2019

That's it, I should use "tcp4" instead of "tcp" if I want it raise a error, otherwise, a dual-stack passive-open socket will work. XD

@chengxuncc chengxuncc closed this Feb 20, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.