我正在尝试同时从多个接口捕获网络数据包。我可以使用下面提到的代码执行此操作,但如果我连接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();
}