473,382 Members | 1,392 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,382 software developers and data experts.

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
4 5362
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Daniel | last post by:
after opening socket, sending data then closing socket 3000 times i get "Only one usage of each socket address" what am i doing wrong? is there some thing else i need to do to free up the socket...
4
by: yaron | last post by:
Hi, I have a problem when sending data over TCP socket from c# client to java server. the connection established ok, but i can't send data from c# client to java server. it's work ok with...
7
by: Bob Garbados | last post by:
I need to construct an xml document, send it to a service over tcp/ip to a specified port, receive the xml response, and process the xml response. I can create the xml document to send, but what's...
1
by: Eric Sheu | last post by:
Greetings, I have been searching the web like mad for a solution to my SMTP problem. I am using Windows Server 2003 and ASP.NET 2.0 w/ C# to send out e-mails from a web site I have created to...
9
by: thorley | last post by:
Greetings, since there was no reponse to my previous post about an existing FastCGI server in python, I've taken to writing my own. (which of course I'll share--*if* there's something to share ;) ...
9
by: Miro | last post by:
VB 2003 at the end of the code, this works great. bytCommand = Encoding.ASCII.GetBytes("testing hello send text") udpClient.Send(bytCommand, bytCommand.Length) and this recieves it Dim...
3
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the...
0
by: Buddy Home | last post by:
There is two examples of code. Example 1. Send and Receive within the same process. Put this code in a console app called SendAndReceive and run the code. using System; using...
0
by: toto1980 | last post by:
Hi; I tried look for a simple way of sending a file using sockets, I found one in the old forums but I am not sure why the file when it is sent, the client side some how receive wrong contents, and...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.