如下代码,经常回提示 System.Net.Sockets.SocketException (0x80004005)
的错误
由于目标计算机积极拒绝,无法连接。 127.0.0.1:80
System.Net.Sockets.SocketException (0x80004005): 由于目标计算机积极拒绝,无法连接。 127.0.0.1:80
public HttpData GetData(IPEndPoint address, byte[] headersData) { HttpData data = null; Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); socket.ReceiveTimeout = receiveTimeout; socket.SendTimeout = sendTimeout; socket.Connect(address.Address, address.Port); socket.Send(headersData); data = GetData(socket); socket.Shutdown(SocketShutdown.Both); socket.Disconnect(true); } catch (Exception ex) { if (socket.Connected) { socket.Close(); } socket.Dispose(); } return data; }
我查看了下端口占用情况
TIME_WAIT 的就这么多
TCP 127.0.0.1:80 127.0.0.1:65321 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65323 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65326 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65327 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65328 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65330 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65332 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65333 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65335 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65336 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65337 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65338 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65339 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65340 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65341 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65342 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65343 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65345 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65346 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65347 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65348 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65349 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65350 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65351 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65352 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65353 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65354 TIME_WAIT 0 TCP 127.0.0.1:80 127.0.0.1:65355 TIME_WAIT 0
ESTABLISHED 状态的就非常多了
不知道如何解决?
有开防火墙或其它安全软件吗?
开了
@foxidea:
会不会是自己把自己给封掉了?
关了也一样
@foxidea:
网上找了下,说是因为socket被占用,还没关闭的情况下再尝试打开就会出现这个错误。你这个函数会多次调用吗?
@XiaoFaye: 是多次调用的函数
socket.Shutdown(SocketShutdown.Both);
socket.Disconnect(true);
我已经调用 这两句了,为什么还是会提示那样的错误呢?
@foxidea:
在socket.Disconnect(true);后面再加个socket.Close();试试,不要只在发生错误后再执行。
看起来像你的Socket没有完全关闭导致的
socket.Shutdown(SocketShutdown.Both);
socket.Disconnect(true);
这是没关闭么?
@foxidea: ESTABLISHED的意思是建立连接。表示两台机器正在通信。你的ESTABLISHED很多,表示连接均未关闭!