gpt4 book ai didi

C# 套接字异常 : an attempt was made to access a socket in a way forbidden by its access permissions

转载 作者:行者123 更新时间:2023-11-30 20:59:40 27 4
gpt4 key购买 nike

这是我正在使用的代码。这是我从网上得到的代码,他们说它工作正常。对此的评论也很好,但我不明白为什么它对我不起作用。还有一件事,我将此应用程序用作用户模式而不是管理员模式。

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);
}
}

最佳答案

And one more thing I am using this application as user mode not in a administrator mode.

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

To use a socket of type SOCK_RAW requires administrative privileges. Users running Winsock applications that use raw sockets must be a member of the Administrators group on the local computer, otherwise raw socket calls will fail with an error code of WSAEACCES. On Windows Vista and later, access for raw sockets is enforced at socket creation. In earlier versions of Windows, access for raw sockets is enforced during other socket operations.

(我的重点)

关于C# 套接字异常 : an attempt was made to access a socket in a way forbidden by its access permissions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15378876/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com