473,623 Members | 2,433 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 5396
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
3786
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 connection, recieve data, and close socket
4
8196
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
5550
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: Dim tcpClient As New System.Net.Sockets.TcpClient tcpClient.Connect("127.0.0.1", 10000) Dim...
1
8175
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 have written to send a test message: -----Code Begins: Sensitive Information Replaced by -----...
9
17981
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 them down the pipe, and then listen for a response. socket.send, as best I can tell, will only...
9
4907
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
4287
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
3156
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
1641
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): /////////////////////////Receiving a file/////////////////////// Code: FILE *fp; char name="t.txt";
0
8227
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8165
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8613
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8469
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7150
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5561
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4164
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1778
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1473
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.