467,888 Members | 1,509 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,888 developers. It's quick & easy.

sending a C# struct through sockets to a c++ client

Hi,
I am writing a server in C# and client in C++ (pure, not managed). The
communication goes on through sockets. In C# I am using NetworkStream to
send text data (by converting it to byte array) to the c++ client. In c++
client I have "recv" (winsock) function through which I receive everything.
Now there is a need to pass a "struct" of C# through the "write" function of
network stream. I want to know how this can be done. Also I would want to
reconstruct the struct at the c++ side after receving data through recv.
Please use a simple struct with few ints to demo this.

Regards,

Ab.
Dec 29 '05 #1
  • viewed: 4673
Share:
4 Replies
Me
You will probably have to manually "create" a byte structure in C# and put
the data into it since C# and C/C++ have a binary notion of a structure
while C# does not.

What I mean is:

C/C++
struct
{
int a;
char tmp[5];
};

This is formatted in memory something like (not exactly) this: 4 bytes for
the int followed by 5 bytes for the char array.

In C# this is not the same since all of the variables (including the struct
which is now a class) is an object. It is not just a few bytes stored in
memory but instead is a bunch of data and the methods that go with it (aka
an object.)

You will probably have to create a member function in the C# code that will
take all of your member variables and put them into a byte array in the same
order that C/C++ would store them in. You should then be able to send them
over to the C/C++ client and cast it to a struct.

One other thing to keep in mind.. You probably will not be able to just send
your struct only - you will probably want to put a small header onto it that
tells the remote (client/server) how big the packet it. Otherwise if you get
several packets right in a row and perfrom a Recv() and it all in and try to
cast it to a struct it will bomb out.

Hope this helps.
Again, I could be wrong on this so if I am just b-slap me and call me
silly!! :-)
"Abubakar" <ab*******@gmail.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
Hi,
I am writing a server in C# and client in C++ (pure, not managed). The
communication goes on through sockets. In C# I am using NetworkStream to
send text data (by converting it to byte array) to the c++ client. In c++
client I have "recv" (winsock) function through which I receive
everything.
Now there is a need to pass a "struct" of C# through the "write" function
of
network stream. I want to know how this can be done. Also I would want to
reconstruct the struct at the c++ side after receving data through recv.
Please use a simple struct with few ints to demo this.

Regards,

Ab.

Dec 30 '05 #2
Me
Another thought on this.

Dont try sending a struct at all....it is probably more of a headache then
you think.

What I would do is create my own packet type to begin with something like
this:

<packet size><packet data>

<packet size> represents how large the <packet data> portion is.
<packet data> is the data that is sent with this packet. This is the data
from your struct but in a different format.

I would suggest maybe XML or something similar for the <packet data> format.
This way you can use it for different systems and if the data changes (maybe
you added new data) it will not effect the person who recv's it since they
will only pick out what is needed. This will also make it easy to process in
both C# and C/C++ code since it is in a "universal" format.

Keep in mind that I am not sure if your system needs to be scaleable or not
so XML may not be a valid solution. If it isn't then go back to my earlier
suggestion and make it a binary string that you created on the C# side.
"Me" <me@home.com> wrote in message
news:96******************************@comcast.com. ..
You will probably have to manually "create" a byte structure in C# and put
the data into it since C# and C/C++ have a binary notion of a structure
while C# does not.

What I mean is:

C/C++
struct
{
int a;
char tmp[5];
};

This is formatted in memory something like (not exactly) this: 4 bytes for
the int followed by 5 bytes for the char array.

In C# this is not the same since all of the variables (including the
struct which is now a class) is an object. It is not just a few bytes
stored in memory but instead is a bunch of data and the methods that go
with it (aka an object.)

You will probably have to create a member function in the C# code that
will take all of your member variables and put them into a byte array in
the same order that C/C++ would store them in. You should then be able to
send them over to the C/C++ client and cast it to a struct.

One other thing to keep in mind.. You probably will not be able to just
send your struct only - you will probably want to put a small header onto
it that tells the remote (client/server) how big the packet it. Otherwise
if you get several packets right in a row and perfrom a Recv() and it all
in and try to cast it to a struct it will bomb out.

Hope this helps.
Again, I could be wrong on this so if I am just b-slap me and call me
silly!! :-)
"Abubakar" <ab*******@gmail.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
Hi,
I am writing a server in C# and client in C++ (pure, not managed). The
communication goes on through sockets. In C# I am using NetworkStream to
send text data (by converting it to byte array) to the c++ client. In c++
client I have "recv" (winsock) function through which I receive
everything.
Now there is a need to pass a "struct" of C# through the "write" function
of
network stream. I want to know how this can be done. Also I would want to
reconstruct the struct at the c++ side after receving data through recv.
Please use a simple struct with few ints to demo this.

Regards,

Ab.


Dec 30 '05 #3
Thanks, I will try these hacks :)

I think I should post the question to the vc ng as well.

Ab.
http://joehacker.blogspot.com

"Me" <me@home.com> wrote in message
news:96******************************@comcast.com. ..
You will probably have to manually "create" a byte structure in C# and put
the data into it since C# and C/C++ have a binary notion of a structure
while C# does not.

What I mean is:

C/C++
struct
{
int a;
char tmp[5];
};

This is formatted in memory something like (not exactly) this: 4 bytes for
the int followed by 5 bytes for the char array.

In C# this is not the same since all of the variables (including the struct which is now a class) is an object. It is not just a few bytes stored in
memory but instead is a bunch of data and the methods that go with it (aka
an object.)

You will probably have to create a member function in the C# code that will take all of your member variables and put them into a byte array in the same order that C/C++ would store them in. You should then be able to send them
over to the C/C++ client and cast it to a struct.

One other thing to keep in mind.. You probably will not be able to just send your struct only - you will probably want to put a small header onto it that tells the remote (client/server) how big the packet it. Otherwise if you get several packets right in a row and perfrom a Recv() and it all in and try to cast it to a struct it will bomb out.

Hope this helps.
Again, I could be wrong on this so if I am just b-slap me and call me
silly!! :-)
"Abubakar" <ab*******@gmail.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
Hi,
I am writing a server in C# and client in C++ (pure, not managed). The
communication goes on through sockets. In C# I am using NetworkStream to
send text data (by converting it to byte array) to the c++ client. In c++ client I have "recv" (winsock) function through which I receive
everything.
Now there is a need to pass a "struct" of C# through the "write" function of
network stream. I want to know how this can be done. Also I would want to reconstruct the struct at the c++ side after receving data through recv.
Please use a simple struct with few ints to demo this.

Regards,

Ab.


Dec 30 '05 #4
Me <me@home.com> wrote:
Dont try sending a struct at all....it is probably more of a headache then
you think.

What I would do is create my own packet type to begin with something like
this:

<packet size><packet data>

<packet size> represents how large the <packet data> portion is.
<packet data> is the data that is sent with this packet. This is the data
from your struct but in a different format.


<snip>

If you know what you're expecting, of course, you don't need the packet
size part. However, I agree that explicitly sending the data is a good
idea. It makes the data format very obvious, and keeps your code robust
against seemingly innocuous things like reordering your variable
declarations.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 30 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Bob Garbados | last post: by
9 posts views Thread by thorley | last post: by
9 posts views Thread by Miro | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.