我試圖在啓用了頭文件的原始套接字上讀取傳入數據包。查看其他項目,比如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