UdpClient接收所有数据 [英] UdpClient Receive all data

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

问题描述

我写了下面的c#Multicast发送/接收。它工作正常,但经过几个小时的研究和尝试多种方法,我一直无法弄清楚如何接收我所有的UDP有效载荷。



 UdpClient udpBroadcast =  new  System.Net.Sockets.UdpClient( 2362 ); 
IPEndPoint groupEp = new IPEndPoint(IPAddress.Parse( 224.0.5.128), 2362 );
udpBroadcast.Connect(groupEp);

byte [] bData = new byte [] {0x44,0x49,0x47,0x49,0x00,0x01,0x00,0x06,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; // Magic Word
udpBroadcast.Send(bData,bData.Length);
udpBroadcast.Close();

UdpClient udpResponse = new System.Net.Sockets.UdpClient( 2362 ) ;
IPEndPoint groupREp = new IPEndPoint(IPAddress.Any, 2362 );

字节 [] rMessage = new 字节 [ 4096 ];
rMessage = udpResponse.Receive( ref groupREp);
string returnData = Encoding.ASCII.GetString(rMessage, 0 ,rMessage.Length);
Console.WriteLine( 发现设备:);
Console.WriteLine(groupREp.Address.ToString()+ 端口: + groupREp .Port.ToString());
Console.WriteLine(returnData.ToString());

udpResponse.Close();





以下是魔术时设备的完整回复单词''被发送,我通过我的代码收到的唯一部分是DIGI块(用Wireshark打开测试)。

 0000 c8 60 00 ea ba 7c 00 40 9d 2d e0 77 08 00 45 00 .` ... |。@ .-。w..E。 
0010 00 9b 00 0d 00 00 40 11 39 f1 c0 a8是01 c0 a8 ...... @。 9 .......
0020 01 02 09 3a 09 3a 00 87 bb b6 44 49 47 49 00 02 ...:。:.. .. DIGI ..
0030 00 77 01 06 00 40 9d 2d e0 77 02 04 c0 a8 be 01 .w ... @ .-。w ......
0040 03 04 ff ff ff 00 0b 04 c0 a8 be 01 06 01 00 0d ........ ........
0050 14 50 6f 72 74 53 65 72 76 65 72 20 54 53 20 31 .PortSer ver TS 1
0060 36 20 4d 45 49 07 01 02 08 1d 56 65 72 73 69 6f 6 MEI ... ..Versio
0070 6e 20 38 32 30 30 30 36 38 34 5f 54 20 30 38 2f n 820006 84_T 08 /
0080 31 38 2f 32 30 30 36 0e 04 00 00 03 03 13 04 00 18/2006。 ........
0090 00 04 03 19 01 01 0f 04 00 00 00 00 10 04 00 00 ........ ........
00a0 00 00 12 01 10 0c 02 00 01 ........





我真的需要整个交易,因为mac地址,ip,服务器设置等都存储在那里。我知道如何阅读它们,但我不知道如何从接收上的c#获取整个数据。



任何建议/帮助?

解决方案

所以期间你运行这个代码whireshark显示整个数据报,但你的udpclinet并没有得到全部? rMessage还只包含那个,或者解码后的字符串只被分块?



你得到的是不是一个字符串,它是某种类型结构 - 因此解码将失败。尝试将其映射到一个结构,或者提取你需要的部分并单独解码它们。



(与你的比较:http://lamahashim.blogspot.hu/2009/06/using-c-udpclient-send-and-receive .html [ ^ ])

I''ve written the below c# Multicast send/receive. It works fine, but after hours and hours of researching and attempting multiple methods, I have been unable to figure out how to receive all of my UDP payload.

UdpClient udpBroadcast = new System.Net.Sockets.UdpClient(2362); 
IPEndPoint groupEp = new IPEndPoint(IPAddress.Parse("224.0.5.128"), 2362);
udpBroadcast.Connect(groupEp);

byte[] bData = new byte[] { 0x44, 0x49, 0x47, 0x49, 0x00, 0x01, 0x00, 0x06, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; //Magic Word
udpBroadcast.Send(bData, bData.Length);
udpBroadcast.Close();

UdpClient udpResponse = new System.Net.Sockets.UdpClient(2362);
IPEndPoint groupREp = new IPEndPoint(IPAddress.Any, 2362);

    Byte[] rMessage = new Byte[4096];
    rMessage = udpResponse.Receive(ref groupREp);
    string returnData = Encoding.ASCII.GetString(rMessage, 0, rMessage.Length);
    Console.WriteLine("Device discovered:");
    Console.WriteLine(groupREp.Address.ToString() + " Port:" + groupREp.Port.ToString());
    Console.WriteLine(returnData.ToString());

udpResponse.Close();



Below is the full response from the devices when the ''magic word'' is sent, the only part I receive back via my code is the "DIGI" chunk (testing with Wireshark open).

0000  c8 60 00 ea ba 7c 00 40  9d 2d e0 77 08 00 45 00   .`...|.@ .-.w..E.
0010  00 9b 00 0d 00 00 40 11  39 f1 c0 a8 be 01 c0 a8   ......@. 9.......
0020  01 02 09 3a 09 3a 00 87  bb b6 44 49 47 49 00 02   ...:.:.. ..DIGI..
0030  00 77 01 06 00 40 9d 2d  e0 77 02 04 c0 a8 be 01   .w...@.- .w......
0040  03 04 ff ff ff 00 0b 04  c0 a8 be 01 06 01 00 0d   ........ ........
0050  14 50 6f 72 74 53 65 72  76 65 72 20 54 53 20 31   .PortSer ver TS 1
0060  36 20 4d 45 49 07 01 02  08 1d 56 65 72 73 69 6f   6 MEI... ..Versio
0070  6e 20 38 32 30 30 30 36  38 34 5f 54 20 30 38 2f   n 820006 84_T 08/
0080  31 38 2f 32 30 30 36 0e  04 00 00 03 03 13 04 00   18/2006. ........
0090  00 04 03 19 01 01 0f 04  00 00 00 00 10 04 00 00   ........ ........
00a0  00 00 12 01 10 0c 02 00  01                        ........ .  



I really need the whole deal, as mac address, ip, server settings, etc. are all stored there. I know how to read them, but I have no clue how to get at the whole data from c# on the Receive.

Any suggestions/help?

解决方案

So during you run this code whireshark is showing the whole datagram, but your udpclinet isn''t getting all? The rMessage also contains only that, or the decoded string is chunked only?

What you get is not a string, it is some sort of structure - thus decoding will fail. Try to map it to a structure, or extract the portions you need and decode them individually.

(Compare this with yours: http://lamahashim.blogspot.hu/2009/06/using-c-udpclient-send-and-receive.html[^])


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

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