抓取的数据包,解析出来总是有乱码啊

wo554006164 2014-02-20 04:45:14
只帖出关键代码:

//解析接收的数据包,形成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

现在打开保存后的文件,里面总会有乱码

...全文
1233 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wo554006164 2014-02-20
  • 打赏
  • 举报
回复
我已经试过以下几种编码方式: Encoding.Default、Encoding.UTF8、Encoding.GetEncoding("GB2312")、Encoding.Unicode、Encoding.ASCII 都有乱码,好像不是编码问题,求解
wo554006164 2014-02-20
  • 打赏
  • 举报
回复
多帖点儿解析数据结果: UDP 4: 192.168.103.106:137==>>192.168.255.255:137 包长度:78 头长度:20 数据长度:58 包:E NO? @B0括gj括 ??:效e EBEOEEFCEFFHCNFAEDCACACACACACAAA :urn:schemas-upnp-org:device:InternetGatewayDevice:1 Man:"ssdp:discover" MX:3 n Referer: http://125.70.0.29:807/crm/main Accept-Encoding: gzip, deflate User-Agent: Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko Host: 125.70.0.29:807 Connection: Keep-Alive Cookie: isPlaySound=true; CRM_LASTLOGINUSERKEY=%20%u5F20%u535A; wnd=false; userlike_0000000017=index; ASP.NET_SessionId=hqr3lzquwmcafoegfgalxphx; userlike_0000000018=index g t y p e n v a r c h a r ( 2 ) , @ o p e r n v a r c h a r ( 5 ) , @ o p e r i p n v a r c h a r ( 9 ) , @ o p e r t i m e d a t e t i m e @ l o g t i t l e ? ? (u7b頞9e N OD崣e@ m o d u l e ? ? OXT-N胈- D崣et@ l o g c o n t e n t ? ? 4 (u7b頞9e N OD崣e酧o`D崣e鱏: 1 4 (u7b鱏9 3 @ l o g t y p e ? ? 頞9e@ o p e r ? ? a d m i n @ o p e r i p ? ? 1 2 7 . 0 . 0 . 1 @ o p e r t i m e o丌 m& s d i s a b l e & @ n e t w o r k d i s k u r l ? ? a s d f f f @ d i r e c t i o n t y p e & @ d a t a u r l 鏧 ? X / u p l o a d s / o t h e r / 2 0 1 4 0 2 2 0 / 2 0 1 4 0 2 2 0 0 0 0 0 0 0 4 6 . p d f @ d a t a s i z e &`? @ b f i n f o ? ? @ c l i c k n u m & @ i d & = ' 2 0 1 4 - 0 2 - 2 0 1 6 : 2 3 : 3 5 ' , [ L a s t l o g i n i p ] = ' 1 2 7 . 0 . 0 . 1 '
wo554006164 2014-02-20
  • 打赏
  • 举报
回复
前几位为什么是乱码呢

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧