我试图在启用了头文件的原始套接字上读取传入数据包。查看其他项目,比如CodeProject上的MJsniffer,我已经能够创建自己的代码来阅读我想要的一切。问题:我只是在检索正在外出的信息。这里是我的代码来初始化一个原始套接字,加工代码在这一点无关..使用IP协议中的原始套接字在C#中读取传入数据包
// Resolve the host name or IP address to am IPHostEntry instance
IPHostEntry hIPHostEntry = Dns.GetHostEntry(Dns.GetHostName());
// Initialize a new instance of the Socket class.
Socket hSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Unspecified); // IP is possible.
// Bind the socket to each resolved IP address.
foreach (IPAddress hIPAddress in hIPHostEntry.AddressList) try { hSocket.Bind(new IPEndPoint(hIPAddress, 0)); } catch(Exception) { continue; }
// Configure the incoming socket to accept all the required information.
hSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);
// Configure the incoming socket to receive all the required information.
hSocket.IOControl(IOControlCode.ReceiveAll, BitConverter.GetBytes(1), BitConverter.GetBytes(1));
// Return the configured socket.
return hSocket;
运行在Windows 7 64位,我有充分的管理权限,我怎么能修改此代码以获取套接字能够读取传入的数据包?传出很好,我也需要,但我绝对需要传入数据包。
P.S:我不想强制用户安装WinPcap。我不想捕获或欺骗或任何东西,只是阅读,这应该是可能的。
感谢分享!它现在对我特别没有帮助,但是对于未来以及对于这个问题偶然发现的其他人来说,这是很好的知识。 – 2012-12-16 22:57:11