C# 试图以访问权限禁止的方式访问套接字。为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10461257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:58:31  来源:igfitidea点击:

an attempt was made to access a socket in a way forbbiden by its access permissions. why?

c#socketsvideostreaming

提问by andrew

 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

我不明白为什么......它起作用了,但现在不起作用了。有人可以帮助我吗?我正在使用 vlc 进行流式传输,但我不想接收数据包,做一些报告,然后将本地重新流式传输到播放器

采纳答案by stark

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

很可能套接字由某个进程持有。用netstat -o找哪一个。

回答by Jonathan

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!

使用管理员权限重新加载 Visual Studio。Windows 套接字 (WinSock) 将不允许您在没有本地管理员的情况下创建 SocketType.RAW 套接字。请记住,您的解决方案需要提升权限才能按预期运行!

回答by Hopeless

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 -oor 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.

好吧,我什至不明白这个问题的罪魁祸首。但就我而言,问题完全不同。我试过运行netstat -onetstat -ab,两者都表明当前没有任何应用程序正在侦听端口 62434,这是我的应用程序尝试侦听的端口。所以这对我来说真的很困惑。

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.

我只是试着想想我做了什么,让它停止工作(它以前确实工作过)。那么我想到了我在我的以太网适配器上使用私有虚拟 LAN 进行的 Internet 共享(在 Windows 10 中使用 Hyper-v)。我只需要关闭共享,它又可以正常工作了。

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.

希望这可以帮助其他有同样问题的人。当然,如果有人可以解释这一点,请在您自己的答案中添加更多详细信息,或者作为对我的答案的评论。

回答by Sunny

I had a similar problem but I fixed it with by doing some changes in 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 "Change Settings" button. Once you clicked on Change Settings button, the "Allow another program..." button gets enabled. enter image description here

  5. Click on "Allow another program..." button , a new dialog box will be opened. Choose the programs or application for which you are getting the socket exception and click on "Add" button. enter image description here

  6. Click OK, and restart your machine.

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

  1. 转到“开始”-->“控制面板”
  2. 点击“Windows防火墙” 在此处输入图片说明

  3. 在 Windows 防火墙中,单击“允许程序或功能通过 Windows 防火墙” 在此处输入图片说明

  4. 现在在允许程序中,单击“更改设置”按钮。单击“更改设置”按钮后,“允许其他程序...”按钮将启用。 在此处输入图片说明

  5. 单击“允许另一个程序...”按钮,将打开一个新对话框。选择您收到套接字异常的程序或应用程序,然后单击“添加”按钮。 在此处输入图片说明

  6. 单击“确定”,然后重新启动机器。

  7. 尝试使用管理权限运行您的应用程序(有一个例外)。

I hope this helps.

我希望这有帮助。

Peace,

和平,

Sunny Makode

桑尼·马科德

回答by Rwt

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.

这是 Windows 防火墙阻止端口(传出)时返回的错误。我们有一个严格的 Web 服务器,因此默认情况下会阻止传出端口。我所要做的就是创建一个规则来允许 wf.msc 中的 TCP 端口号。

回答by Gareth

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

我在尝试使用 dotnet run 启动 dotnet Core 项目时遇到了这个问题,当它尝试绑定到端口时。

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.

问题是由于在项目打开的情况下运行 Visual Studio 2017 实例引起的 - 我之前通过 VS 启动了项目进行调试,看起来它一直在保持端口,即使调试已完成并且应用程序似乎已关闭.

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

关闭 Visual Studio 实例并再次运行“dotnet run”解决了问题。

回答by MFry

Running iisresetin a command window fixed it for me.

iisreset在命令窗口中运行为我修复了它。

回答by wip

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.

当我在两个不同的 Visual Studio 实例中有两个不同的 ASP.net 项目时,发生了同样的错误。
关闭其中一个解决了这个问题。

回答by stimms

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 /devstorageaccount1here is a dead giveaway.

尝试连接到 Blob 存储时,我在 Azure 上的 Web 应用程序中遇到了这个问题。问题原来是我错过了为 blob 存储部署连接字符串,所以它仍然指向存储模拟器。客户端必须内置一些重试逻辑,因为我看到了大约 3 次尝试。这/devstorageaccount1是一个致命的赠品。

enter image description here

在此处输入图片说明

Fixed by properly setting the connection string in Azure.

通过在 Azure 中正确设置连接字符串来修复。

回答by Finitely Failed

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

我正在开发一个 UWP 应用程序,它连接到 LAN 中的 MQTT 代理。我去一个类似的错误。

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 capabilites ... enter image description here

原来我忘了给应用程序正确的功能...... 在此处输入图片说明