抓取的数据包,解析出来总是有乱码啊
只帖出关键代码:
保存时的代码:
保存后的数据:
UDP 4: 192.168.8.114:-4151==>>239.255.255.250:1900 包长度:456 头长度:20 数据长度:436
数据:E 萚} 爴括r??l?襈OTIFY * HTTP/1.1
HOST: 239.255.255.250:1900
CACHE-CONTROL: max-age=100
LOCATION: http://192.168.8.114:49152/TxMediaRenderer_desc.xml
OPT: "http://schemas.upnp.org/upnp/1/0/"; ns=01
01-NLS: 3594a4b1-99e6-11e3-a8d7-e3fa26daac3c
NT: upnp:rootdevice
NTS: ssdp:alive
SERVER: 6.1.7601 2/Service Pack 1, UPnP/1.0, Portable SDK for UPnP devices/1.6.17
X-User-Agent: redsonic
USN: uuid:f46d04cd931e0000_MR::upnp:rootdevice
://192.168.4.132:158/admin/main.aspx
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8
Cookie: CookieName=u=2EE46A9445F246E446E202CE054FF10EB8AD2242C8C57B790B4379F6357CFBA298E6090E6B82E58A; time=t=20042249D04B4986A4E22E5BF089EE65E4A703E03E44F8FE; ASP.NET_SessionId=foaeodd1vp4u3cu502psh3lt; ecs_s_s_ui_ll_op=NBXmO8ikPTY=; ecs_s_s_un_ll_op=V7bnX7F2g0Q=; ecs_s_s_up_ll_op=tZuExlRygrMPkv48Ke3RCMSTQKMhcmz9kccJ8Xx8FtNmBYUmz7nAFw==; ecs_s_s_si_ll_op=281A8v43EPM=
type=loadorderonotherdata
现在打开保存后的文件,里面总会有乱码
//解析接收的数据包,形成PacketArrivedEventArgs事件数据类对象,并引发PacketArrival事件
unsafe private void Receive(byte[] buf, int len)
{
byte temp_protocol = 0;
uint temp_version = 0;
uint temp_ip_srcaddr = 0;
uint temp_ip_destaddr = 0;
short temp_srcport = 0;
short temp_dstport = 0;
IPAddress temp_ip;
PacketArrivedEventArgs e = new PacketArrivedEventArgs();//新网络数据包信息事件
fixed (byte* fixed_buf = buf)
{
IPHeader* head = (IPHeader*)fixed_buf;//把数据流整和为IPHeader结构
e.HeaderLength = (uint)(head->ip_verlen & 0x0F) << 2;
temp_protocol = head->ip_protocol;
switch (temp_protocol)//提取协议类型
{
case 1: e.Protocol = "ICMP"; break;
case 2: e.Protocol = "IGMP"; break;
case 6: e.Protocol = "TCP"; break;
case 17: e.Protocol = "UDP"; break;
default: e.Protocol = "UNKNOWN"; break;
}
temp_version = (uint)(head->ip_verlen & 0xF0) >> 4;//提取IP协议版本
e.IPVersion = temp_version.ToString();
//以下语句提取出了PacketArrivedEventArgs对象中的其他参数
temp_ip_srcaddr = head->ip_srcaddr;
temp_ip_destaddr = head->ip_destaddr;
temp_ip = new IPAddress(temp_ip_srcaddr);
e.OriginationAddress = temp_ip.ToString();
temp_ip = new IPAddress(temp_ip_destaddr);
e.DestinationAddress = temp_ip.ToString();
temp_srcport = *(short*)&fixed_buf[e.HeaderLength];
temp_dstport = *(short*)&fixed_buf[e.HeaderLength + 2];
e.OriginationPort = IPAddress.NetworkToHostOrder(temp_srcport).ToString();
e.DestinationPort = IPAddress.NetworkToHostOrder(temp_dstport).ToString();
e.PacketLength = (uint)len;
e.MessageLength = (uint)len - e.HeaderLength;
e.ReceiveBuffer = buf;
//把buf中的IP头赋给PacketArrivedEventArgs中的IPHeaderBuffer
Array.Copy(buf, 0, e.IPHeaderBuffer, 0, (int)e.HeaderLength);
//把buf中的包中内容赋给PacketArrivedEventArgs中的MessageBuffer
Array.Copy(buf, (int)e.HeaderLength, e.MessageBuffer, 0, (int)e.MessageLength);
}
//引发PacketArrival事件
PacketArrival(this, e);
}
保存时的代码:
public static readonly object objLock = new object();
public static void put(PacketArrivedEventArgs args)
{
string filename = AppDomain.CurrentDomain.BaseDirectory + "log/" + DateTime.Now.ToString("yyyy-MM-ddHH") + ".txt";
lock (objLock)
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(filename, true))
{
sw.WriteLine(args.Protocol + " " + args.IPVersion + ": " + args.OriginationAddress + ":" + args.OriginationPort + "==>>" + args.DestinationAddress + ":" + args.DestinationPort + " 包长度:" + args.PacketLength + " 头长度:" + args.HeaderLength + " 数据长度:" + args.MessageLength);
sw.WriteLine("数据:" + System.Text.Encoding.Default.GetString(args.ReceiveBuffer));
sw.WriteLine("===========================================================================");
}
}
}
保存后的数据:
UDP 4: 192.168.8.114:-4151==>>239.255.255.250:1900 包长度:456 头长度:20 数据长度:436
数据:E 萚} 爴括r??l?襈OTIFY * HTTP/1.1
HOST: 239.255.255.250:1900
CACHE-CONTROL: max-age=100
LOCATION: http://192.168.8.114:49152/TxMediaRenderer_desc.xml
OPT: "http://schemas.upnp.org/upnp/1/0/"; ns=01
01-NLS: 3594a4b1-99e6-11e3-a8d7-e3fa26daac3c
NT: upnp:rootdevice
NTS: ssdp:alive
SERVER: 6.1.7601 2/Service Pack 1, UPnP/1.0, Portable SDK for UPnP devices/1.6.17
X-User-Agent: redsonic
USN: uuid:f46d04cd931e0000_MR::upnp:rootdevice
://192.168.4.132:158/admin/main.aspx
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8
Cookie: CookieName=u=2EE46A9445F246E446E202CE054FF10EB8AD2242C8C57B790B4379F6357CFBA298E6090E6B82E58A; time=t=20042249D04B4986A4E22E5BF089EE65E4A703E03E44F8FE; ASP.NET_SessionId=foaeodd1vp4u3cu502psh3lt; ecs_s_s_ui_ll_op=NBXmO8ikPTY=; ecs_s_s_un_ll_op=V7bnX7F2g0Q=; ecs_s_s_up_ll_op=tZuExlRygrMPkv48Ke3RCMSTQKMhcmz9kccJ8Xx8FtNmBYUmz7nAFw==; ecs_s_s_si_ll_op=281A8v43EPM=
type=loadorderonotherdata
现在打开保存后的文件,里面总会有乱码
...全文
相关推荐
springcloud-gateway获取应用响应信息乱码
springcloud-gateway获取应用响应信息乱码
python抓取并保存html页面时乱码问题的解决方法
主要介绍了python抓取并保存html页面时乱码问题的解决方法,结合实例形式分析了Python页面抓取过程中乱码出现的原因与相应的解决方法,需要的朋友可以参考下
Node.js抓取中文网页乱码问题和解决方法
主要介绍了Node.js抓取中文网页乱码问题和解决方法,本文讲解了使用一些开源库解决抓取中出现的乱码问题,需要的朋友可以参考下
nodejs爬虫抓取数据乱码问题总结
主要给大家总结了下nodejs爬虫抓取数据乱码问题的相关资料,需要的朋友可以参考下
详解Python解决抓取内容乱码问题(decode和encode解码)
一、乱码问题描述 经常在爬虫或者一些操作的时候,经常会出现中文乱码等问题,如下 原因是源网页编码和爬取下来后的编码格式不一致 二、利用encode与decode解决乱码问题 字符串在Python内部的表示是unicode编码,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(encode)成另一种编码。 decode的作用是将其他编码的字符串转换成unicode编码,如str1.decode(‘gb2312′),表示将gb2312编码的字符串str1转换成unicode编码。 encode的作用是将un


