Solved

C# - UDP (UdpClient)  Raw Packet - Constructing data payload?

Posted on 2006-07-08
4
2,974 Views
Last Modified: 2008-01-16
Hi,

I'm building a UDP server/client program using UdpClient in C#.
Its 'send' (byte[] , int length, IPAddress dest, int port) method allows me to send bytes to the other side.  However, what I really want to do is I want to send multiple pieces of data to the other side.  I therefore would like to have a data struture which contains the data which I would like to send and then send this data, thus I need to explicitly construct the UDP payload myself.  Therefore, the payload must be split into multiple data items to send to the other side.

I've looked over books and documentation but can't seem to find the answer?

Therefore, I was wondering how you would implement this?

Thanks in advance for your help!
0
Comment
Question by:malben
  • 2
4 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 50 total points
ID: 17065136
It sounds like you are looking for a way to marshal a structure to a byte array:

Marshaling Structures
http://www.codeproject.com/vb/net/Marshaling_Structures.asp

C# equivalent:

private struct Test
{
  short Var1;
  short Var2
}

//Start here
Test tst = new Test();

// initialize Structure (Dummy  Values)
tst.Var1 = 911;
tst.Var2 = 7;

IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(tst));
byte[] byteArray = new byte[Marshal.SizeOf(tst)];

//Now copy structure to pointer
Marshal.StructureToPtr(tst, ptr, false);
Marshal.Copy(ptr, byteArray, 0, Marshal.SizeOf(tst));

//Now use ByteArray ready for use
Marshal.FreeHGlobal(ptr);

Bob
0
 

Author Comment

by:malben
ID: 17065833
Hi,

Would be possible to use serialization with structs?

thanks
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 17067167
Yes you can serialize a struct. Are you not trying to make your data as small as possible for transmission over the wire? If so you should be able to use the above example.

http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/msg/b19f6b810baa3315 offers a few ready to go solutions as well.

Cheers,

Greg
0
 

Author Comment

by:malben
ID: 17067730
Thanks guys for your help.  I will give it a try!

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now