473,577 Members | 3,256 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5390
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*******@gmai l.com> wrote in message
news:%2******** *******@TK2MSFT NGP09.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*******@gmai l.com> wrote in message
news:%2******** *******@TK2MSFT NGP09.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*******@gmai l.com> wrote in message
news:%2******** *******@TK2MSFT NGP09.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.co m>
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
3782
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 after i send data into it? I simply want to open socket, send data, close socket and have the server just handle one client thread to recieve...
4
8194
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 TcpClient, NetworkStream and StreamWriter classes. but with low level socket it doesn't work (When using the Socket class Send method).
7
5548
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 the best way to move the data from an XmlDocument to a Byte array? Currently I paste the xml into a textbox and send it with the following code: ...
1
8168
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 the members of my organization. I think my problem is incorrectly setting the settings on my server or an authentication problem. Here is the code I...
9
17973
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 ;) My problem now, is that I need to send certain binary data over a socket. That is, I want to make some bytes, and stuff them in a TCP packet, send...
9
4888
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 strReturnData As String = _ System.Text.Encoding.ASCII.GetString(receiveBytes)
3
4280
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 receiver is running within Process1. If I move the receiver into Process2 then its fast. Please can someone explain.
0
3152
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 System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Runtime.Serialization.Formatters.Binary;
0
1639
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 when having a big file let us say 200 KB, the file transfer is not complete. Here is the code: Client side (receiver):...
0
7845
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
7776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8286
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...
1
7869
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8143
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6517
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
5664
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...
1
2287
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
1385
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.