Send Hex code by UDP
-
Hello, I'm pretty au fait with VB.NET but a complete novice to programming anything to do with networking. I'm trying to replicate the behaviour of one small part of an application we have so that I can build my own application which will be much simpler for our staff to use to perform that one function. The application is a server program. Anyway, using various network sniffers I've identified the packets that the application sends. It seems that when it starts up it broadcasts a UDP packet over the network, and then the clients respond with ICMP echos, after which further TCP packets are sent to the clients. Well, I'm stuck at the first hurdle! I need, to begin with, to replicate that initial UDP broadcast. Using .NET and lots of Googling I've been able to produce code which will send a UDP packet to the right IP and port. What I'm struggling with is the data of the packet. Because I have the packet as captured in SoftPerfect Network Protocol Analyser (much more user-friendly than Wireshark for my money) I know exactly what the packet should look like and I can even copy the hex of the original packet. I can't then simply stick this hex code into a string and send it over UDP, it doesn't work. Presumably it needs to be in some kind of array, but all the references I can find to this online seem to involve CONVERTING a string to hex or hex to bytes. Surely since I already have the hex no conversion should be necessary? I just want to send it exactly as it is! When I did convert it to a byte array (if that's the right term, this is very new to me) and then sent it the network sniffer picked it up and the data in the packet was very different and much longer than the original one I'm trying to replicate. Any help or advice would be greatly appreciated, from general tips about replicating network packets to actual code I can copy! Thanks in advance.
-
Hello, I'm pretty au fait with VB.NET but a complete novice to programming anything to do with networking. I'm trying to replicate the behaviour of one small part of an application we have so that I can build my own application which will be much simpler for our staff to use to perform that one function. The application is a server program. Anyway, using various network sniffers I've identified the packets that the application sends. It seems that when it starts up it broadcasts a UDP packet over the network, and then the clients respond with ICMP echos, after which further TCP packets are sent to the clients. Well, I'm stuck at the first hurdle! I need, to begin with, to replicate that initial UDP broadcast. Using .NET and lots of Googling I've been able to produce code which will send a UDP packet to the right IP and port. What I'm struggling with is the data of the packet. Because I have the packet as captured in SoftPerfect Network Protocol Analyser (much more user-friendly than Wireshark for my money) I know exactly what the packet should look like and I can even copy the hex of the original packet. I can't then simply stick this hex code into a string and send it over UDP, it doesn't work. Presumably it needs to be in some kind of array, but all the references I can find to this online seem to involve CONVERTING a string to hex or hex to bytes. Surely since I already have the hex no conversion should be necessary? I just want to send it exactly as it is! When I did convert it to a byte array (if that's the right term, this is very new to me) and then sent it the network sniffer picked it up and the data in the packet was very different and much longer than the original one I'm trying to replicate. Any help or advice would be greatly appreciated, from general tips about replicating network packets to actual code I can copy! Thanks in advance.
All packet data is just an arrray of bytes. Since you have the value of every byte, all you have to do is recreate that data in an array. It's that data that is going to get sent in the packet. Even if you supply a String, each character in the string if converted to a byte or set of bytes and sent as part of the packet. You have to values for each byte, they're just presented to you in Hexadecimal form.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
Hello, I'm pretty au fait with VB.NET but a complete novice to programming anything to do with networking. I'm trying to replicate the behaviour of one small part of an application we have so that I can build my own application which will be much simpler for our staff to use to perform that one function. The application is a server program. Anyway, using various network sniffers I've identified the packets that the application sends. It seems that when it starts up it broadcasts a UDP packet over the network, and then the clients respond with ICMP echos, after which further TCP packets are sent to the clients. Well, I'm stuck at the first hurdle! I need, to begin with, to replicate that initial UDP broadcast. Using .NET and lots of Googling I've been able to produce code which will send a UDP packet to the right IP and port. What I'm struggling with is the data of the packet. Because I have the packet as captured in SoftPerfect Network Protocol Analyser (much more user-friendly than Wireshark for my money) I know exactly what the packet should look like and I can even copy the hex of the original packet. I can't then simply stick this hex code into a string and send it over UDP, it doesn't work. Presumably it needs to be in some kind of array, but all the references I can find to this online seem to involve CONVERTING a string to hex or hex to bytes. Surely since I already have the hex no conversion should be necessary? I just want to send it exactly as it is! When I did convert it to a byte array (if that's the right term, this is very new to me) and then sent it the network sniffer picked it up and the data in the packet was very different and much longer than the original one I'm trying to replicate. Any help or advice would be greatly appreciated, from general tips about replicating network packets to actual code I can copy! Thanks in advance.
Hi, there is nothing in TCP/IP or UDP that uses hex. It is all bytes, representing numbers; numbers aren't decimal or hex or octal, they are just a bunch of bits. Hex/decimal/octal is a human way to look at numbers, and it only exists as a string. Your sniffer is getting bytes, and showing them to you using a hex string (because that has a fixed length of 2 characters per byte), you should read each 2-character hex number as one byte value. So if your sniffer sees "12CD" that would be two bytes, one with hex representation "12" (hence decimal value 18), and one "CD" (i.e. 205). So you would need a 2-element byte array, initialize it with 18 and 205 (or &12H and &CDH) and send it. :)
Luc Pattyn
Local announcement (Antwerp region): Lange Wapper? Neen!
-
Hi, there is nothing in TCP/IP or UDP that uses hex. It is all bytes, representing numbers; numbers aren't decimal or hex or octal, they are just a bunch of bits. Hex/decimal/octal is a human way to look at numbers, and it only exists as a string. Your sniffer is getting bytes, and showing them to you using a hex string (because that has a fixed length of 2 characters per byte), you should read each 2-character hex number as one byte value. So if your sniffer sees "12CD" that would be two bytes, one with hex representation "12" (hence decimal value 18), and one "CD" (i.e. 205). So you would need a 2-element byte array, initialize it with 18 and 205 (or &12H and &CDH) and send it. :)
Luc Pattyn
Local announcement (Antwerp region): Lange Wapper? Neen!
-
That's really useful, I think I understand now, thank you. But given what you've said what code would I need to actually convert my long string of hex code into a byte array that I can send by UDP?
Ok, with a lot more Googling and trial and error I think I finally cracked it!
Dim sText As String = "00 00 84 00 00 00 00 02 00 00 00 00 0A 4D 61 67 69 63 69 6E 66 6F 53 06 4D 4C 49 4E 4B 53 04 5F 75 64 70 05 6C 6F 63 61 6C 00 00 0C 00 01 00 00 00 00 00 1E 1C 31 33 38 32 35 33 31 35 32 30 39 31 30 30 39 32 30 37 4D 61 67 69 63 69 6E 66 6F 53 00 C0 17 00 0C 00 01 00 00 00 1E 00 02 C0 34" Dim sData() As String Dim iByte As Long, lByteCount As Long sData = Split(Trim(sText), " ") lByteCount = UBound(sData) + 1 Dim BData(lByteCount - 1) As Byte For iByte = 0 To lByteCount - 1 BData(iByte) = CByte("&H" & sData(iByte)) Next iByte