简体   繁体   English

在我的C#嗅探器中设置套接字选项时,不知道为什么我收到错误代码10022(无效参数)

[英]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.

相关问题 C#套接字错误10022 - C# socket error 10022 我正在用 Unity 研究 C# 构造函数和析构函数,但我不知道为什么它会这样工作 - I'm studying the C# constructor and destructor with Unity, but I don't know why it works like this 当运行基本的c#控制台应用程序时,控制台打开空白,我不知道为什么 - When running my basic c# console app, my console opens blank and I don't know why 为什么当我使用RegisterStartupScript或RegisterClientScriptBlock时,我的C#代码不起作用? - Why when I use RegisterStartupScript or RegisterClientScriptBlock my C# code don't works? C#无效的Cast异常(我不知道错误在哪里) - C# Invalid Cast exception (I don't know where is the error) 出于某种原因,我的代码不应该在我的僵尸中产生,我不知道为什么 - For some reason my code is spawning in my zombies when it is not supposed to and I don't know why 我正在尝试运行一个循环,但我遇到了一个我不知道如何修复的错误 - I'm trying to run a loop, and I'm getting an error which I don't know how to fix 我不断收到此错误消息,但我不知道为什么 - I keep getting this error message and I don't know why 我不知道为什么我在温度的索引13处得到“ 0” - I don't know why I'm getting “0” at index 13 of temp 反序列化我的XML时,出现错误消息XML无效 - When deserializing my XML, I'm getting an error that XML is invalid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM