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

 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


Solution 1:

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:

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

Solution 3:

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:

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:

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"