为什么在`ConnectionRefused`之后为未使用的本地主机/端口组合获取`AddressAlreadyInUse`?

时间:2017-06-29 06:11:28

标签: .net sockets

当连接到没有服务器正在侦听的主机/端口时,我得到ConnectionRefused,这很好。但是,当以1分钟的间隔重试(同时启动)服务器时,我得到AddressAlreadyInUse

这似乎是由于绑定到相同的host / ip组合。但是没有使用端口,netstat没有显示任何内容,甚至没有TIME_WAIT状态或任何会产生AddressAlreadyInUse的东西。究竟是什么造成了这个?

代码如下:

var bindEndPoint = new IPEndPoint(192.168.60.60, 55001);
var endPoint = new IPEndPoint(192.168.50.54, 55001);

while (true)
{
  try
  {
    log("Connecting to " + endPoint + " from " + bindEndPoint);
    var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    socket.Bind(bindEndPoint);
    socket.LingerState = new LingerOption(true, 0);
    socket.Connect(endPoint);
    log("Connection succeeded");
    DoStuffAsLongAsConnected();
  }
  catch (Exception e)
  {
    log("Connection failed: Socket Error " + e.ErrorCode + " " + e.SocketErrorCode + Environment.NewLine + e);
  }
  Thread.Sleep(60 * 1000);
}

日志:

14:37:42.581 192.168.60.60:55001->192.168.50.54:55001 Connecting to 192.168.50.54:55001 from 192.168.60.60:55001
14:37:43.597 192.168.60.60:55001->192.168.50.54:55001 Connection failed: Socket Error 10061 ConnectionRefused
14:38:43.428 192.168.60.60:55001->192.168.50.54:55001 Connection failed: Socket Error 10048 AddressAlreadyInUse
14:39:44.243 192.168.60.60:55001->192.168.50.54:55001 Connecting to 192.168.50.54:55001 from 192.168.60.60:55001
14:39:44.243 192.168.60.60:55001->192.168.50.54:55001 Connection failed: Socket Error 10048 AddressAlreadyInUse
14:40:45.155 192.168.60.60:55001->192.168.50.54:55001 Connecting to 192.168.50.54:55001 from 192.168.60.60:55001
14:40:45.155 192.168.60.60:55001->192.168.50.54:55001 Connection failed: Socket Error 10048 AddressAlreadyInUse
14:41:46.083 192.168.60.60:55001->192.168.50.54:55001 Connecting to 192.168.50.54:55001 from 192.168.60.60:55001
14:41:46.083 192.168.60.60:55001->192.168.50.54:55001 Connection failed: Socket Error 10048 AddressAlreadyInUse
14:42:46.561 192.168.60.60:55001->192.168.50.54:55001 Connecting to 192.168.50.54:55001 from 192.168.60.60:55001
14:42:46.561 192.168.60.60:55001->192.168.50.54:55001 Connection failed: Socket Error 10048 AddressAlreadyInUse
14:43:46.904 192.168.60.60:55001->192.168.50.54:55001 Connecting to 192.168.50.54:55001 from 192.168.60.60:55001
14:43:46.904 192.168.60.60:55001->192.168.50.54:55001 Connection failed: Socket Error 10048 AddressAlreadyInUse
14:44:47.309 192.168.60.60:55001->192.168.50.54:55001 Connecting to 192.168.50.54:55001 from 192.168.60.60:55001
14:44:47.309 192.168.60.60:55001->192.168.50.54:55001 Connection failed: Socket Error 10048 AddressAlreadyInUse
14:45:47.698 192.168.60.60:55001->192.168.50.54:55001 Connecting to 192.168.50.54:55001 from 192.168.60.60:55001
14:45:47.698 192.168.60.60:55001->192.168.50.54:55001 Connection failed: Socket Error 10048 AddressAlreadyInUse
14:46:48.087 192.168.60.60:55001->192.168.50.54:55001 Connecting to 192.168.50.54:55001 from 192.168.60.60:55001
14:46:48.087 192.168.60.60:55001->192.168.50.54:55001 Connection failed: Socket Error 10048 AddressAlreadyInUse
14:47:48.673 192.168.60.60:55001->192.168.50.54:55001 Connecting to 192.168.50.54:55001 from 192.168.60.60:55001
14:47:48.673 192.168.60.60:55001->192.168.50.54:55001 Connection failed: Socket Error 10048 AddressAlreadyInUse
14:48:49.331 192.168.60.60:55001->192.168.50.54:55001 Connecting to 192.168.50.54:55001 from 192.168.60.60:55001
14:48:49.331 192.168.60.60:55001->192.168.50.54:55001 Connection failed: Socket Error 10048 AddressAlreadyInUse
14:49:50.036 192.168.60.60:55001->192.168.50.54:55001 Connecting to 192.168.50.54:55001 from 192.168.60.60:55001
14:49:50.036 192.168.60.60:55001->192.168.50.54:55001 Connection failed: Socket Error 10048 AddressAlreadyInUse
14:50:50.662 192.168.60.60:55001->192.168.50.54:55001 Connecting to 192.168.50.54:55001 from 192.168.60.60:55001
14:50:50.662 192.168.60.60:55001->192.168.50.54:55001 Connection succeeded

1 个答案:

答案 0 :(得分:0)

尽管Connect()中存在异常,但我必须在创建新的.Dispose()之前调用Socket并将其绑定到同一本地IP /端口,然后不AddressAlreadyInUse。 1}}异常被抛出。

@o_weisman感谢提示 - 重用现有的Socket可能是另一种选择。