Is there API for get a free TCP port as web server testing process?

2,032 views
Skip to first unread message

dlin

unread,
May 14, 2013, 3:53:44 AM5/14/13
to golan...@googlegroups.com
I want to write a test program to test web server which register a free port.

But, when we Listening, we must provide a parameter e.g :6060.

Because port 6060 is occupied by godoc sometimes, I wish my  server_test.go will run as soon as possible.
So, I require to assign a free port number.

Any suggestion?

Tamás Gulácsi

unread,
May 14, 2013, 5:11:45 AM5/14/13
to golan...@googlegroups.com
Use 0 as port number.

Jim Teeuwen

unread,
May 14, 2013, 5:12:22 AM5/14/13
to golan...@googlegroups.com
Use Port 0. The operating system will then assign you a free one automatically.
You can see which one was chosen through Listener.Address(), after it has opened.

dlin

unread,
May 14, 2013, 5:28:57 AM5/14/13
to golan...@googlegroups.com
Thank you, 
In fact, 
In my test, not only the web server required a free port.

I required multiple clients assigned by local port number.
Is there API to get the free local port number for the clients to use?

At the first test, the test will pass.
At 2nd test, the client dialer Dial() to server will caused 'address already used' error.
And use 'netstat -tan | grep <port>' will see it is on TIME_WAIT state.
So, I think to let the auto test smooth is to get a free local port at first.

lu dongping

unread,
May 14, 2013, 5:50:50 AM5/14/13
to dlin, golang-nuts
l,_ := net.Listen("tcp",":0")
defer l.Close()
println(l.Addr().String())





--
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...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

dlin

unread,
May 14, 2013, 7:05:18 AM5/14/13
to golan...@googlegroups.com, dlin
Thanks, I just use "" empty string. And it works.
Reply all
Reply to author
Forward
0 new messages