[英]Don't know why I'm getting Error Code 10022 (Invalid Argument) when setting socket options in my C# sniffer
I'm writing a packet sniffer as an exercise in learning .Net 4 socket development on in C#. 我正在编写一个数据包嗅探器,作为在C#中学习.Net 4套接字开发的练习。 My goal is to sniff IP packets coming in and out out my computer. 我的目标是嗅探进出我的计算机的IP数据包。
My problem is that I'm getting error code 10022, invalid argument, on my call to SetSocketOption. 我的问题是我在调用SetSocketOption时收到错误代码10022,参数无效。 I don't see where I have an invalid argument. 我没有看到我的论点无效。 I have some admin privs on my computer, but perhaps I don't have enough. 我的计算机上有一些管理员权限,但也许我没有足够的管理权限。 It's my work computer and the IT department is pretty strict. 这是我的工作计算机,IT部门非常严格。 With that said, if it was a permissions problem I would expect a different exception. 话虽如此,如果是权限问题,我会期待一个不同的例外。
I'm not sure what my next step should be to debug this problem. 我不确定我的下一步应该是调试这个问题。 Anyone have an idea? 有人有想法吗?
Here's the code follows: 这是代码如下:
public Sniffer()
{
try
{
socket = new Socket(
AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
IPAddress[] ipAddresses = Dns.GetHostEntry(
Dns.GetHostName()).AddressList;
socket.Bind(new IPEndPoint(ipAddresses[0], 0));
socket.SetSocketOption(
SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);
byte[] inputData = new byte[4] { 1, 0, 0, 0 };
byte[] outValue = new byte[4];
socket.IOControl(IOControlCode.ReceiveAll, inputData, outValue);
}
catch (SocketException ex)
{
string ErrorMessage = ex.Message;
}
}
Due to abuse by viruses in the early 2000's Microsoft has restricted the use of raw sockets on non server editions of the the windows OS on all OS's newer than XP SP2. 由于2000年初被病毒滥用,微软已经限制在所有操作系统上比XP SP2更新的Windows操作系统的非服务器版本上使用原始套接字。
You can read more on what restrictions are in plance from the page on TCP/IP Raw Sockets on the MSDN. 您可以从MSDN上的TCP / IP原始套接字页面上了解更多有关限制的内容。
WSAEINVAL 10022 WSAEINVAL 10022
Invalid argument. 无效的论点。
Some invalid argument was supplied (for example, specifying an invalid level to the setsockopt function). 提供了一些无效参数(例如,为setsockopt函数指定了无效级别)。 In some instances, it also refers to the current state of the socket—for instance, calling accept on a socket that is not listening. 在某些情况下,它还引用套接字的当前状态 - 例如,在未侦听的套接字上调用accept。
Take a look about this error here: http://msdn.microsoft.com/en-us/library/ms740668(v=vs.85).aspx 看看这个错误: http : //msdn.microsoft.com/en-us/library/ms740668(v = vs。85).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.