473,385 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

marshalling socket buffer

I have an async socket callback for a UDP socket. When the callback is fired after receiving a packet, I want to map the packet to a structure I have.

note, all unsafe code blocks here.

// note, i know the following is not an entire DHCP packet, I just didn't want this msg to get too long.
[StructLayout(LayoutKind.Explicit)]
public struct DHCPPacket
{
[FieldOffset(0)] public byte op;
[FieldOffset(1)] public byte htype;
[FieldOffset(2)] public byte hlen;
[FieldOffset(3)] public byte hops;
[FieldOffset(4)] public uint xid;
[FieldOffset(8)] public ushort secs;
[FieldOffset(10)] public ushort flags;
}

When coming into this routine I only have the asyncstate available to me from the iasyncresult which I get through

unsafe public void MessageReceived( IAsyncResult ar )
{
int length = m_Socket.EndReceive( ar );

Debug.WriteLine( "Bytes received " + length.ToString() + ".");

byte[] buffer = (byte[])ar.AsyncState;
fixed( byte *inbuff = buffer )
{
Debug.WriteLine( "next line wont compile" );
DHCPPacket * packet = (DHCPPacket *)inbuff; // this line won't compile despite examples I've seen where it should

// call processing of packet here
}

StartReceivingData();
}
the problem is that despite many examples I've seen where you can map your unsafe structure to a pointer, the compiler doesn't seem to want to let me. What am I doing wrong? For that matter, is there another easy way to get from point a to point b
where I have DHCPPacket mapped to the memory area?
Nov 16 '05 #1
2 1933
ok, I figured our a way that works pretty well.

DHCPPacket packet = (DHCPPacket)Marshal.PtrToStructure( (IntPtr)inbuff, typeof( DHCPPacket ) );

seems to do just the trick.
On Fri, 04 Feb 2005 14:58:13 -0700, allen <al****@sparkysystems.com> wrote:
I have an async socket callback for a UDP socket. When the callback is fired after receiving a packet, I want to map the packet to a structure I have.

note, all unsafe code blocks here.

// note, i know the following is not an entire DHCP packet, I just didn't want this msg to get too long.
[StructLayout(LayoutKind.Explicit)]
public struct DHCPPacket
{
[FieldOffset(0)] public byte op;
[FieldOffset(1)] public byte htype;
[FieldOffset(2)] public byte hlen;
[FieldOffset(3)] public byte hops;
[FieldOffset(4)] public uint xid;
[FieldOffset(8)] public ushort secs;
[FieldOffset(10)] public ushort flags;
}

When coming into this routine I only have the asyncstate available to me from the iasyncresult which I get through

unsafe public void MessageReceived( IAsyncResult ar )
{
int length = m_Socket.EndReceive( ar );

Debug.WriteLine( "Bytes received " + length.ToString() + ".");

byte[] buffer = (byte[])ar.AsyncState;
fixed( byte *inbuff = buffer )
{
Debug.WriteLine( "next line wont compile" );
DHCPPacket * packet = (DHCPPacket *)inbuff; // this line won't compile despite examples I've seen where it should

// call processing of packet here
}

StartReceivingData();
}
the problem is that despite many examples I've seen where you can map your unsafe structure to a pointer, the compiler doesn't seem to want to let me. What am I doing wrong? For that matter, is there another easy way to get from point a to point b
where I have DHCPPacket mapped to the memory area?


Nov 16 '05 #2
One word of caution though: there is no guarantee that the full packet has
arrived when the system calls MessageReceived. The number of bytes arrived
could be anything between 1 to sizeof(DHCPPacket), or 0 if the connection
was closed. To make it robust you should handle the situation when the
packet arrives in multiple fragments.

Regards,
Sami

"allen" <al****@sparkysystems.com> wrote in message
news:v7********************************@4ax.com...
ok, I figured our a way that works pretty well.

DHCPPacket packet = (DHCPPacket)Marshal.PtrToStructure( (IntPtr)inbuff,
typeof( DHCPPacket ) );

seems to do just the trick.
On Fri, 04 Feb 2005 14:58:13 -0700, allen <al****@sparkysystems.com>
wrote:
I have an async socket callback for a UDP socket. When the callback is
fired after receiving a packet, I want to map the packet to a structure I
have.

note, all unsafe code blocks here.

// note, i know the following is not an entire DHCP packet, I just didn't
want this msg to get too long.
[StructLayout(LayoutKind.Explicit)]
public struct DHCPPacket
{
[FieldOffset(0)] public byte op;
[FieldOffset(1)] public byte htype;
[FieldOffset(2)] public byte hlen;
[FieldOffset(3)] public byte hops;
[FieldOffset(4)] public uint xid;
[FieldOffset(8)] public ushort secs;
[FieldOffset(10)] public ushort flags;
}

When coming into this routine I only have the asyncstate available to me
from the iasyncresult which I get through

unsafe public void MessageReceived( IAsyncResult ar )
{
int length = m_Socket.EndReceive( ar );

Debug.WriteLine( "Bytes received " + length.ToString() + ".");

byte[] buffer = (byte[])ar.AsyncState;
fixed( byte *inbuff = buffer )
{
Debug.WriteLine( "next line wont compile" );
DHCPPacket * packet = (DHCPPacket *)inbuff; // this line won't compile
despite examples I've seen where it should

// call processing of packet here
}

StartReceivingData();
}
the problem is that despite many examples I've seen where you can map your
unsafe structure to a pointer, the compiler doesn't seem to want to let
me. What am I doing wrong? For that matter, is there another easy way to
get from point a to point b
where I have DHCPPacket mapped to the memory area?

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Brian Rice | last post by:
I have a socket application that is sending and receiving packets asynchronously. It works great except, when I receive packets that are larger than my receive buffer which then generate several...
4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
7
by: Colin | last post by:
I'm writing a little console socket server but I'm having some difficulty. Can I ask your advice - where is the best place to get some help on that topic? It would be nice if some people who knew...
13
by: coloradowebdev | last post by:
i am working on basically a proxy server that handles requests via remoting from clients and executes transactions against a third-party server via TCP. the remoting site works like a champ. my...
9
by: Macca | last post by:
Hi, I have a synchronous socket server which my app uses to read data from clients. To test this I have a simulated client that sends 100 byte packets. I have set up the socket server so...
2
by: Macca | last post by:
My app has an asynchronous socket server. It will have 20 clients connected to the server. Each client sends data every 500 millisecondsThe Connections once established will not be closed unless...
0
by: Macca | last post by:
Hi, I am writing an asychronous socket server to handle 20+ simulataneous connections. I have used the example in MSDN as a base. The code is shown at end of question. Each connection has a...
3
by: Cheryl | last post by:
Hi. I am having a problem on handling asynchronous sockets in C#. I implemented a pair of client and server sockets. The connection is ok when first connected. However, when I turned off the...
1
by: keksy | last post by:
Hi every1, I am writing a small client/server application and in it I want to send an image asynchronous from the client to the server through a TCP socket. I found an example code on the MSDN...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.