【问题标题】:C# Socket Exception : an attempt was made to access a socket in a way forbidden by its access permissionsC# 套接字异常:试图以访问权限禁止的方式访问套接字
【发布时间】:2013-03-13 07:00:33
【问题描述】:

这是我正在使用的代码。这是我从互联网上获得的代码,他们说它工作正常。这方面的 cmets 也不错,但我不明白为什么它不适合我。还有一件事我将此应用程序用作用户模式而不是管理员模式。

private void btnStart_Click(object sender, EventArgs e)
    {
        if (cmbInterfaces.Text == "")
        {
            MessageBox.Show("Select an Interface to capture the packets.", "MJsniffer", 
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }
        try
        {
            if (!bContinueCapturing)        
            {
                //Start capturing the packets...

                btnStart.Text = "&Stop";

                bContinueCapturing = true;

                //For sniffing the socket to capture the packets has to be a raw socket, with the
                //address family being of type internetwork, and protocol being IP
                Console.WriteLine("1");
                mainSocket = new Socket(AddressFamily.InterNetwork,
                    SocketType.Raw, ProtocolType.IP);
                Console.WriteLine("2");
                //Bind the socket to the selected IP address
                mainSocket.Bind(new IPEndPoint(IPAddress.Parse(cmbInterfaces.Text), 0));
                Console.WriteLine("3");
                //Set the socket  options
                mainSocket.SetSocketOption(SocketOptionLevel.IP,            //Applies only to IP packets
                                           SocketOptionName.HeaderIncluded, //Set the include the header
                                           true);                           //option to true
                Console.WriteLine("4");
                byte[] byTrue = new byte[4] {1, 0, 0, 0};
                byte[] byOut = new byte[4]{1, 0, 0, 0}; //Capture outgoing packets

                //Socket.IOControl is analogous to the WSAIoctl method of Winsock 2
                mainSocket.IOControl(IOControlCode.ReceiveAll,              //Equivalent to SIO_RCVALL constant
                                                                            //of Winsock 2
                                     byTrue,                                    
                                     byOut);

                //Start receiving the packets asynchronously
                mainSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None,
                    new AsyncCallback(OnReceive), null);
            }
            else
            {
                btnStart.Text = "&Start";
                bContinueCapturing = false;
                //To stop capturing the packets close the socket
                mainSocket.Close ();
            }
        }
        catch (SocketException ex)
        {
            Console.WriteLine("5");
            MessageBox.Show(ex.Message, "MJsniffer", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        catch (Exception ex)
        {
            Console.WriteLine("6");
            MessageBox.Show(ex.Message, "MJsniffer", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

【问题讨论】:

  • 哪行代码导致问题标题中提到的异常?

标签: c# sockets


【解决方案1】:

还有一件事,我将此应用程序用作用户模式而不是管理员模式。

这行不通。 following 是为 Win32 api 编写的,但由于这是 .NET 调用的内容,因此同样适用:

使用SOCK_RAW 类型的套接字需要管理权限。运行使用原始套接字的 Winsock 应用程序的用户必须是本地计算机上管理员组的成员,否则原始套接字调用将失败,错误代码为 WSAEACCES。在 Windows Vista 及更高版本上,对原始套接字的访问是在创建套接字时强制执行的。在早期版本的 Windows 中,对原始套接字的访问是在其他套接字操作期间强制执行的。

我的重点

【讨论】:

    【解决方案2】:

    您能否检查 SocketException.SocketErrorCode 并更新您的问题?
    我假设您收到 10013 - 这些是 code descriptions。 很可能其他应用程序已经在访问套接字,或者您的权限丢失,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 2011-02-16
      相关资源
      最近更新 更多