?
Solved

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

Posted on 2006-07-08
4
Medium Priority
?
3,041 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 150 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

801 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