【发布时间】:2011-01-30 03:07:15
【问题描述】:
我正在尝试通过侦听原始套接字通过代码读取来自网站的响应,但到目前为止,我只能读取我的计算机发送的传出请求,而不是我实际上的传入响应有兴趣。 我该如何阅读收到的回复?
编辑:我相信使用 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]
);
}
}
【问题讨论】:
-
new IPEndPoint(localIP, 0) 指定要监听端口 0。是远程服务器连接的端口吗?
-
@RobertLevy -
new IPEndPoint(localIP, 0)指定您不关心您使用的本地端口。当(如果)您需要一个时,系统会选择一个。 (绑定、发送、接收……)对于设置了 ReceiveAll 的 RAW 套接字,您永远不需要一个,因为您正在接收所有内容。
标签: c# raw-sockets