C#获取分组数据 [英] C# Getting packet data

查看:387
本文介绍了C#获取分组数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图写一个脚本,将嗅探HTTP标头。到目前为止,我已经得到了套接字绑定到端口80和包似乎接受,但我不能让他们进入字符串形式。所有这一切是输出E不断。我换了个字节为十六进制更早,似乎有一些数据进来的,但目前的代码是无法改变的字节为一个字符串。是否有将给予适当的字符串解码字节的一些其他的方式?



 字节[] =输入BitConverter.GetBytes(1 ); 
字节[]缓冲区=新的字节[4096];
插座S =新的Socket(AddressFamily.InterNetwork,SocketType.Raw,ProtocolType.IP);
s.Bind(新IPEndPoint(IPAddress.Parse(地带),80));
s.IOControl(IOControlCode.ReceiveAll,输入,NULL);
INT字节;
字节= s.Receive(缓冲);
而(字节大于0)
{
日志(System.Text.Encoding.ASCII.GetString(缓冲液,0,字节));
字节= s.Receive(缓冲);
}


解决方案

当你闻使用数据的原始的插座,您收到互联网协议(IP)的数据包。每个IP数据包始于一个 IP头。此标头通常为20字节长,但也可以是长于。以下的IP报头的传输层报头,例如,在传输控制协议(TCP)头或用户数据报协议(UDP)头。这种头部后面就是你要找的数据,即HTTP。因此,当你解析数据,你需要跳过IP报头和传输层报头第一位。


I've been trying to write a script that will sniff HTTP headers. So far I've got the socket bound to port 80 and packets seem to be received, but I can't get them into string form. All that outputs is "E" continuously. I changed the bytes into hex earlier and there seems to be some data coming in, but the current code is unable to change the bytes into a string. Is there some other way of decoding the bytes that will give a proper string?

byte[] input = BitConverter.GetBytes(1);
byte[] buffer = new byte[4096];
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
s.Bind(new IPEndPoint(IPAddress.Parse(strIP), 80));
s.IOControl(IOControlCode.ReceiveAll, input, null);
int bytes;
bytes = s.Receive(buffer);
while (bytes > 0)
{
    log(System.Text.Encoding.ASCII.GetString(buffer, 0, bytes));
    bytes = s.Receive(buffer);
}

解决方案

When you sniff data using a raw socket, you're receiving Internet Protocol (IP) packets. Each IP packet begins with an IP header. This header is typically 20 bytes long, but it can be longer than that. Following the IP header is the header for the Transport Layer, e.g., the Transmission Control Protocol (TCP) header or the User Datagram Protocol (UDP) header. After this header comes the data you're looking for, i.e., the HTTP. So when you're parsing the data, you need to skip past the IP header and the Transport Layer header first.

这篇关于C#获取分组数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆