Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-07-08
4
Medium Priority
?
3,095 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

609 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