Trying to manipulate the contents of a packet. (micro framework v3.0)

Hello guys,

I am trying to send a packet of information(256bytes) by using UDP to a data logger.

so far i know to send a message. here is the code.

byte[] messageBytes = Encoding.UTF8.GetBytes("");
clientSocket.SendTo(messageBytes, serverEndPoint)

to send an object, here is another code.

byte[] test = Reflection.Serialize(kid1, typeof(Kid))
clientSocket.SendTo(test, serverEndPoint)

The data logger I am trying to communicate with receives Packet Contents in the attached picture.

My question is: How do I send the first 12 bytes in the type of string, then the rest of bytes in the type of integer in a packet?

I have thought of using a “structure” but i have no idea how to continue. meanwhile, i am still thinking about how to do it. :’(

This is not C so you cannot cast raw bytes to a structure.

I suggest you do this: create a class that will represent your data packet and will include all those string/int properties. If you want to send this packet over the wire you need to create a method that will return a byte array filled with values taken from data packet properties. E.g. you add 12 bytes of string property, 4 bytes of each int property etc.

When you receive those bytes over the wire you need to do the oposite. Create a new object of data packet class and initialize its properties acording to what was received. E. g. you take first 12 received bytes, convert them to string (Encoding.UTF8.GetString) and set the property on data packet object.

You could use serialization (i didn’t use it on net mf yet - didn’t event know it has that) but it will require a lot more data to be send.

Check out the source code of my netbios class. It contains a few examples of what you want to do.

http://code.tinyclr.com/project/244/name-service---give-your-fez-a-real-network-name/

F.e. see the Header class (and its Parse / ToArray methods)

Parse / ToArray methods that Wouter suggested are exactly what i had in mind.

It might be better to use a struct rather than a class if you are doing a lot of these

Coming from C++ and clean binary structures, working binary structures in C# is a pain.

Here is a quick example of what you could do. As I am far to be a C# expert, this is a working example, but you may have a better way. Interrested into knowing it, though ::slight_smile: !


byte[] MyArray = new byte[256];
byte[] PacketIdentificationHeader = Encoding.UTF8.GetBytes("GRAPHTEC-RD");
Int32 CommunicationID = 0x1234ABCD;

// Start with a clean array filled with 0s
Array.Clear(MyArray, 0, 256);

// First bytes are text, copyed at offset 0
Array.Copy(PacketIdentificationHeader,0,MyArray,0,PacketIdentificationHeader.Length);
            
// Adding an int32 at offset 16, (MSB first)
for (int i = 0; i < 4; i++) MyArray[16 + i] = (byte)((CommunicationID >> (8 * (3-i))) & 0xFF);

// Manualy setting up hex data at offset 20
MyArray[20] = 0x88;
MyArray[21] = 0xAA;
MyArray[22] = 0x55;
MyArray[23] = 0xFF;

// Showing 24 first bytes results in hex
string result="";
for (int i = 0; i < 24; i++) result += (i>0?":":"") + (char)((MyArray[i] >> 4) > 9 ? (MyArray[i] >> 4) + 'A' - 10 : (MyArray[i] >> 4) + '0') + (char)((MyArray[i] & 0xF) > 9 ? (MyArray[i] & 0xF) + 'A' - 10 : (MyArray[i] & 0xF) + '0');
Debug.Print(result);

Results should be printing :

@ Nicolas I agree with you. C# promotes a different approach with its dynamic memory allocation so casting raw bytes to structures can’t be used. On the other hand in regular .NET you have object serialziation to binary/XML where you cant send a far more complex data than just plain structs.

I don’t think so. For example, the struct in the image contains a variable length block (Parameter area). Just let a class manage all that stuff and your code will get nicely separated.

Hello Guys, Thank you all very much for answering my question.

Sorry for replying you late, my internet was down for a while.

Now I have another problem.

I sent a packet which contains the code to activate “ECHO mode” ( so the data logger will reply the same packet to my laptop)

By using a program called “WinDump”, i can see that my laptop sends 256 byte packet to the data logger, and i can see that data logger actually replies with the same length.

However, my program never really receives anything from the data logger because each time the data logger replies to my laptop via DIFFERENT PORT.

the first time it replies via port 60021,
later on, 60023, 60025, 60027 , 60024.

I set my IPEndPoint,

IPEndPoint(IPAdress.Any, 0);

here, by setting the value to 0, supposedly the program can listen to all the ports.

My new question is " How to fix that port of my laptop so the data logger can communicate with my program?"

P.S I did shutdown my firewall and anti-virus software for the experiment.

This is my socket.

the point of using a struct is that you dont stress the gc, you can still add accessor functions the only thing you lose is the ability to inherit

I have never seen that binding to port zero means “receive from any port”. where did you see thar?

When you send to port 8023, your device uses a free local port to send from. So this behavior is normal.

The remote host does not broadcast here, but sends it to your device, to it’s local port it used during sending the message.

In short: just receive the message back on the socket you’ve send the message from…

Hello Mike,

I just googled it, and i checked some codes on some websites.
some of the websites say by setting that value to 0, the socket will listen to all the ports. :o

Hi Wouter Huysentruit,

Could you teach me how, please? :-[

How do I know my sending port during the runtime?

Thank you all for your concern.

Hello guys,

I’ve just corrected my code and now my program can receive the packet from the data logger.

Thank you all again for helping me! :slight_smile: