1

"System.Net.Sockets.SocketException (0x80004005): An attempt was made to access a socket in a way forbidden by its access permissions"

OK I know this problem has occurred many times before on S.O., but I cannot find a solution. Here is my question: what are possible causes of this.

Points:

  • Happens irrespective of firewall
  • Ports are open
  • Destination is open and connections have been working in the past
  • Since working connections, no changes to firewalls or virus scanners, can rule those out.
  • Running as administrative user
  • Source port is 23876
  • Destination port is variable, depends on detected client from another source
  • Windows 8.1

One more point - this error also happened after porting to .NET Core.

The only change is that I am trying to share ports but I can't see the connection between that and the error.The code to connect is

 client = new TcpClient();
                         client.Client.SetSocketOption(
                             SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                         client.Client.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, 0);
                         client.Client.Bind(ipLocalEndPoint);
                         client.Client.Connect(ep);

where the local endpoint is reused.

What are the possible causes of this error? It is happening on every BIND call.

Sentinel
  • 3,351
  • 27
  • 38
  • What port number are you trying to open? What access level (admin, standard user) is your code executing under? What operating system? See [this answer](https://stackoverflow.com/q/2778840/62576) for at least one reason that information is relevant to add to your post. – Ken White Sep 24 '18 at 22:26
  • @KenWhite Thanks Ken, it's all admin mode. Adding info now. – Sentinel Sep 24 '18 at 22:31

1 Answers1

0

OK this all turned out to be me being incredibly stupid.

What I had done, and forgotten I had done, is establish a TcpListener on the same port, with this code _tcpListener.ExclusiveAddressUse = false; not working as I expected.

Sentinel
  • 3,351
  • 27
  • 38