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

A

Abubakar

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.
 
M

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!! :)
 
M

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 said:
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 said:
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.
 
A

Abubakar

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 said:
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 said:
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.
 
J

Jon Skeet [C# MVP]

Me said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top