473,586 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(La youtKind.Explic it)]
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.EndRec eive( 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
}

StartReceivingD ata();
}
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 1947
ok, I figured our a way that works pretty well.

DHCPPacket packet = (DHCPPacket)Mar shal.PtrToStruc ture( (IntPtr)inbuff, typeof( DHCPPacket ) );

seems to do just the trick.
On Fri, 04 Feb 2005 14:58:13 -0700, allen <al****@sparkys ystems.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(La youtKind.Explic it)]
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.EndRec eive( 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
}

StartReceivingD ata();
}
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(DHCPPack et), 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****@sparkys ystems.com> wrote in message
news:v7******** *************** *********@4ax.c om...
ok, I figured our a way that works pretty well.

DHCPPacket packet = (DHCPPacket)Mar shal.PtrToStruc ture( (IntPtr)inbuff,
typeof( DHCPPacket ) );

seems to do just the trick.
On Fri, 04 Feb 2005 14:58:13 -0700, allen <al****@sparkys ystems.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(La youtKind.Explic it)]
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.EndRec eive( 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
}

StartReceivingD ata();
}
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
7581
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 EndReceive calls... these calls seem to be sometimes coming out of order. For example, the following is from my log file: Connection at 5/1/2004...
4
18102
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 as I find appropriate. I am using the socket asynchronously by calling the BeingSend and BeginReceive calls. I would like to be able to call...
7
2372
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 what they were doing could take a look at my code and tell me where and why I'm going wrong. Any suggestions of groups or forums?
13
2637
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 problem is executing the transactions against the remote server and returning the response to the remoting client. i can open the socket fine and,...
9
3591
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 that its buffer is bigger than this. I did this expecting the data to be read in one pass.
2
6864
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 there is a problem with the connection. I need to know which client has sent the incoming data as each client has its own buffer on my "server"...
0
4664
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 number of different types of data coming in. I have a databuffer for each type of data coming in.
3
14389
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 server socket, the client is able to connect, but cannot send anything out. It seems that the Socket.Connected is false but I received no...
1
7143
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 site, which is actually for sending strings. I tried to adapt this code so that the client sends an image instead of a string. However, there is...
0
7912
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.