先给出结论:

同一个进程,使用一个端口,然后连接关闭,大约需要30s后才可再次使用这个端口。

首先使用端口9001连接服务端,发送数据,然后关闭连接,接着再次使用端口9001连接服务端,如果连接失败,间隔15s后,再次尝试,最多尝试3次,。

client

server

output

client输出:

$ ./client
netaddr: :9001
count: 28 msg: you are welcome. i'm server.
=========================
netaddr: :9001
dial failed: dial tcp :9001->127.0.0.1:8080: bind: address already in use

netaddr: :9001
dial failed: dial tcp :9001->127.0.0.1:8080: bind: address already in use

netaddr: :9001
count: 28 msg: you are welcome. i'm server.
=========================

经过3次重试,30s后,才可以重新使用同一个端口9001进行连接。也就是同一个进程的情况狂下,一个连接关闭后,端口大约30s后才可以被使用。