Solved

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

Posted on 2006-07-08
4
3,022 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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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