Solved

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

Posted on 2006-07-08
4
2,988 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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