An attempt was made to access a socket in a way forbidden by its access permissions. Why?

C#SocketsVideoStreaming

C# Problem Overview


 private void StartReceivingData(string ipAddress, int iPort)
    {
        try
        {
            if (!_bContinueReciving)
            {
                //initializeMainSocket(ipAddress, iPort);
                _mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);//<------HERE IS RAISED THE EXCEPTION
                _mSocket.Bind(new IPEndPoint(IPAddress.Parse(ipAddress), iPort));
                //  _mSocket.Bind(new IPEndPoint(IPAddress.Loopback, iPort));
                _mSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);
                _mSocket.IOControl(IOControlCode.ReceiveAll, new byte[4] { 1, 0, 0, 0 }, new byte[4] { 0, 0, 0, 0 });
                //var 1
                _mSocket.BeginReceive(_buffReceivedData, 0, _buffReceivedData.Length, SocketFlags.None,
                                     new AsyncCallback(OnReceive), null);
                initializeLocalSocket();
            }
            else
            {
                _bContinueReciving = false;
                _mSocket.Close();
            }
        }
        catch (Exception exception)
        {
            Debug.WriteLine(exception);
        }
    }

I don't understand why...it worked and now it doesn't. could someone help me? i am streaming with vlc, and i wan't to receive the packets, do some reports, and then restream local to a player

C# Solutions


Solution 1 - C#

I restarted this service : Host Network Service on windows Services program. And it worked.

You can run these commands in the windows terminal instead, as @admin mentioned in comment section:

net stop hns
net start hns

Solution 2 - C#

Most likely the socket is held by some process. Use netstat -o to find which one.

Solution 3 - C#

Reload Visual Studio with Administrator privileges. Windows Sockets (WinSock) will not allow you to create a SocketType.RAW Socket without Local Admin. And remember that your Solution will need elevated privileges to run as expected!

Solution 4 - C#

Well I don't even understand the culprit of this problem. But in my case the problem is totally different. I've tried running netstat -o or netstat -ab, both show that there is not any app currently listening on port 62434 which is the one my app tries to listen on. So it's really confusing to me.

I just tried thinking of what I had made so that it stopped working (it did work before). Well then I thought of the Internet sharing I made on my Ethernet adapter with a private virtual LAN (using Hyper-v in Windows 10). I just needed to turn off the sharing and it worked just fine again.

Hope this helps someone else having the same issue. And of course if someone could explain this, please add more detail in your own answer or maybe as some comment to my answer.

Solution 5 - C#

IIS was the main offender for me. My IIS was running and it restrains any new socket connections from opening. The problem was resolved for me by stopping IIS by running the command "iisreset -stop"

In addition to this, if you use docker, Docker might be the cause of this problem. If so, you have to restart Host Network Service by executing the below command. You may need elevated access to executing this command "net stop hns && net start hns"

Solution 6 - C#

I've had this problem when trying to start a dotnet Core project using dotnet run when it tried to bind to the port.

The problem was caused by having a Visual Studio 2017 instance running with the project open - I'd previously started the project via VS for debugging and it appears that it was holding on to the port, even though debugging had finished and the application appeared closed.

Closing the Visual Studio instance and running "dotnet run" again solved the problem.

Solution 7 - C#

I had a similar problem but I fixed it by doing some changes in the firewall setting.

You can follow the below steps

  1. Go to "Start" --> "Control Panel"

  2. Click on "Windows Firewall" enter image description here

  3. Inside Windows Firewall, click on "Allow a program or feature through Windows Firewall" enter image description here

  4. Now inside of Allow Programs, Click on the "Change Settings" button. Once you click on the Change Settings button, the "Allow another program..." button gets enabled. enter image description here

  5. When you click on the "Allow another program..." button, a new dialog box will appear. Choose the programs or applications for which you are getting the socket exception and click on the "Add" button.

enter image description here

  1. Click OK, and restart your machine.

  2. Try to run your application (which has an exception) with administrative rights.

I hope this helps.

Solution 8 - C#

This is the error that is returned when the Windows Firewall blocks the port (out-going). We have a strict web server so the outgoing ports are blocked by default. All I had to do was to create a rule to allow the TCP port number in wf.msc.

Solution 9 - C#

I ran into this in a Web App on Azure when attempting to connect to Blob Storage. The problem turned out to be that I had missed deploying a connection string for the blob storage so it was still pointing at the storage emulator. There must be some retry logic built into the client because I saw about 3 attempts. The /devstorageaccount1 here is a dead giveaway.

enter image description here

Fixed by properly setting the connection string in Azure.

Solution 10 - C#

I'm developing a UWP application which connects to an MQTT broker in the LAN. I got a similar error.

MQTTnet.Exceptions.MqttCommunicationException: 'An attempt was made to access a socket in a way forbidden by its access permissions [::ffff:xxx.xxx.xxx.xxx]:1883'

ExtendedSocketException: An attempt was made to access a socket in a way forbidden by its access permissions [::ffff:xxx.xxx.xxx.xxx]:1883

Turned out that I forgot to give the app the correct capabilities ... enter image description here

Solution 11 - C#

Running iisreset in a command window fixed it for me.

Solution 12 - C#

When a process uses a port, it cannot be used by another process. netstat -o shows the ports being used by a process.

Alternatively, ports can also be excluded from usage. In that case, no process can use them. You can see the list via netsh interface ipv4 show excludedportrange protocol=tcp

Solution 13 - C#

I've just had a similar problem, from a Xamarin Forms application, which was making an outbound call to Azure via HttpClient.

In my case the root cause of the problem turned out to be my security suite, BitDefender, blocking outbound access for my application, because it thought it was a threat.

I've added an exception to the firewall for this application and it has solved the problem.

Solution 14 - C#

I had the same error happening when I had two different ASP.net projects in two different Visual Studio instances.
Closing one of them fixed the issue.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionandrewView Question on Stackoverflow
Solution 1 - C#ParsaView Answer on Stackoverflow
Solution 2 - C#starkView Answer on Stackoverflow
Solution 3 - C#JonathanView Answer on Stackoverflow
Solution 4 - C#HopelessView Answer on Stackoverflow
Solution 5 - C#Towhidul Islam TuhinView Answer on Stackoverflow
Solution 6 - C#GarethView Answer on Stackoverflow
Solution 7 - C#SunnyView Answer on Stackoverflow
Solution 8 - C#RwtView Answer on Stackoverflow
Solution 9 - C#stimmsView Answer on Stackoverflow
Solution 10 - C#Finitely FailedView Answer on Stackoverflow
Solution 11 - C#MFryView Answer on Stackoverflow
Solution 12 - C#Daniel RoseView Answer on Stackoverflow
Solution 13 - C#Andrew HView Answer on Stackoverflow
Solution 14 - C#wipView Answer on Stackoverflow