I have a loop that opens a socket, acts on the socket, then closes the socket and restarts.
However, on the second iteration I get SocketException
, Only one usage of each socket address (protocol/network address/port) is normally permitted.
However, the socket should be closed, netstat -a
doesn't show that I'm listening on that port or anything. The code that throws the exception is:
_bindedLocalSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_bindedLocalSocket.Bind(new IPEndPoint(Util.ChannelProfileToListeningAddress(_profile), _profile.ListenPort));
_bindedLocalSocket.Listen(30);
_bindedLocalSocket.BeginAccept(new AsyncCallback(OnRequested), null);
However, I think the culprit is not that code, just before I get to that code, and before I try to close the connection I get this:
An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult) at Poderosa.PortForwarding.SynchronizedSocket.EndReceive(IAsyncResult ar) at Poderosa.PortForwarding.Channel.OnSocketData(IAsyncResult result)
Once I close the program and run it again, it can make the first connection fine, the second one acts just the same (SocketException). Does anybody have any idea how I can fix this?