我想通過監聽原始套接字通過代碼從網站讀取響應,但迄今爲止,我只能讀取由我的計算機發送的傳出請求,而不是我真正感興趣的即將到來的回覆。 我該如何去閱讀傳入的回覆?無法使用原始套接字讀取傳入響應
編輯:使用Wireshark我發現我正在尋找的數據正在通過TCP發送,我相信。
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Unspecified);
IPAddress localIP = Dns.GetHostByName(Dns.GetHostName()).AddressList[0];
listener.Bind(new IPEndPoint(localIP, 0));
byte[] invalue = new byte[4] { 1, 0, 0, 0 };
byte[] outvalue = new byte[4] { 1, 0, 0, 0 };
listener.IOControl(IOControlCode.ReceiveAll, invalue, outvalue);
while (true)
{
byte[] buffer = new byte[1000000];
int read = listener.Receive(buffer);
if (read >= 20)
{
Console.WriteLine("Packet from {0} to {1}, protocol {2}, size {3}",
new IPAddress((long)BitConverter.ToUInt32(buffer, 12)),
new IPAddress((long)BitConverter.ToUInt32(buffer, 16)),
buffer[9],
buffer[2] << 8 | buffer[3]
);
}
}
新的IPEndPoint(localIP,0)指定您要偵聽端口0.是否是遠程服務器連接的端口? – 2011-01-30 03:35:43
@RobertLevy - `new IPEndPoint(localIP,0)`指定你不關心你使用的是什麼本地端口。當(如果)你需要一個時,系統將選擇一個。 (綁定,發送,接收,...)對於設置了ReceiveAll的RAW套接字,您永遠不需要一個,因爲您正在接收所有內容。 – 2015-03-23 04:34:31