捕获流经USB调制解调器的网络数据

时间:2014-05-03 11:18:49

标签: c# sockets packet-capture packet-sniffers

我正在尝试同时从多个接口捕获网络数据包。我可以使用下面提到的代码执行此操作,但如果我连接USB调制解调器,则在选择该IP时会出现异常。例外来自行mainSocket.IOControl(IOControlCode.ReceiveAll, byTrue, byOut)

例外是

  

“引用的对象类型不支持尝试的操作。”

class SetIp
{
    private Socket mainSocket;
    private byte[] byteData = new byte[4096];

    public void setIP(IPAddress add)
    {
        mainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
        mainSocket.Bind(new IPEndPoint(add, 0));
        mainSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);
        Thread MyThread = new Thread(new ThreadStart(() =>
        {
            while (true)
            {
                byte[] byTrue = new byte[4] { 1, 0, 0, 0 };
                byte[] byOut = new byte[4] { 1, 0, 0, 0 }; //Capture outgoing packets

                mainSocket.IOControl(IOControlCode.ReceiveAll, byTrue, byOut);

                mainSocket.Receive(byteData, 0, byteData.Length, SocketFlags.None);

                ParseData(byteData, byteData.Length);

            }
        }));
        MyThread.Start();

    }

0 个答案:

没有答案