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

structs for data transfer?

Hello,
I want to transfer messages between a client and a server (over TCP
sockets). A message consists of a message type (like a message "subject"
:), the size of the attached data, and the data itself. The data part
should then be able to contain some information whose layout depends on
the message type...
So now I thought I could define some structs which represent the layout
of the additional information, like this:

typedef struct
{
char versionString[30]
bool paused;
int uptime;
} MsgCoreInfoStruct;

And then I use something like this:

MsgCoreInfoStruct myData;
strcpy(myData.versionString, "Server V1.0");
myData.uptime = getUptime();
myData.paused = false;

Message m;
m.setData( (char*)(&myData) , sizeof(myData) );

The Message object then sends the data to the other side.

Question: When I receive such a message, can I just cast the char*
(which points to the data) into MsgCoreInfoStruct* myRecvData?
And can I then use myRecvData->uptime to get the value I sent out?
And, last but not least ;) , can I use this concept if client and server
are running on different platforms (in this case, Linux and Win)?

I have doubts about this (because of data packing, and because
sizeof(bool) might be inconsistent between compilers...), but I'm not
sure...

Can someone tell me if this concept is right or wrong? Or has someone a
better solution for this problem (maybe something else than structs)?

Thanks in advance,
Oliver Gerlich
Jul 22 '05 #1
8 2127
Oliver Gerlich wrote:
I want to transfer messages between a client and a server (over TCP
sockets).


I did that, many many winters ago, and wish I didn't. When you communicate,
TCP is too low-level for data. It can handle the low-level handshaking and
message metadata required, but not all the high-level details. When you need
them, you will find yourself adding packets to your packets.

You are asking "how can I make my program very inflexible for no reason?"

You need to serialize your data into XML, and then transfer that in a higher
level protocol, such as HTTP. After you go with a pre-existing system like
those, they will answer all of your low-level questions, such as how do I
make this string arbitary length, or how do I add a new field, or how do I
change a field's meaning, or how to I interpret a transmission error, etc.

The C languages specify the memory layout of raw structures. Only use them
when you have a real reason not to use a higher level protocol. Premature
optimization is the root of all evil.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #2

"Oliver Gerlich" <ol************@gmx.de> wrote in message
news:ci*************@news.t-online.com...
Hello,
I want to transfer messages between a client and a server (over TCP
sockets). A message consists of a message type (like a message "subject"
:), the size of the attached data, and the data itself. The data part
should then be able to contain some information whose layout depends on
the message type...
So now I thought I could define some structs which represent the layout of
the additional information, like this:

typedef struct
{
char versionString[30]
bool paused;
int uptime;
} MsgCoreInfoStruct;

And then I use something like this:

MsgCoreInfoStruct myData;
strcpy(myData.versionString, "Server V1.0");
myData.uptime = getUptime();
myData.paused = false;

Message m;
m.setData( (char*)(&myData) , sizeof(myData) );

The Message object then sends the data to the other side.

Question: When I receive such a message, can I just cast the char* (which
points to the data) into MsgCoreInfoStruct* myRecvData?
And can I then use myRecvData->uptime to get the value I sent out?
And, last but not least ;) , can I use this concept if client and server
are running on different platforms (in this case, Linux and Win)?

I have doubts about this (because of data packing, and because
sizeof(bool) might be inconsistent between compilers...), but I'm not
sure...
You are right to doubt.

Can someone tell me if this concept is right or wrong? Or has someone a
better solution for this problem (maybe something else than structs)?

Write some code to convert the struct you want to send into a char array.
Write some code to turn that char array back into a struct. Use the first
piece of code when you send, the second when you recieve. Trying to send
anything more complicated than char arrays between different types of
computers is asking for trouble.
Thanks in advance,
Oliver Gerlich


john
Jul 22 '05 #3
Oliver Gerlich wrote:
I have doubts about this (because of data packing, and because
sizeof(bool) might be inconsistent between compilers...), but I'm not
sure...
Yes you are right :-)
1. You can not be sure not only about bool, but also about byte order(endian
type) and int size.
2. Worst than that, there may be alignament difference into the elements of
the stuct.

What you have to do is to "serialize" data for sending and "deserialize" for
receiving. Doing that means to define some rules about how each data type
is converted into a byte array and back.
Can someone tell me if this concept is right or wrong? Or has someone a
better solution for this problem (maybe something else than structs)?


You can always send a structure safelly, after you have defined a method of
serialization of that struct :-)

If you have time, a good book to read it will be Stevens "Unix Network
Programming" for a clear ideea about sockets.
Jul 22 '05 #4
Phlip wrote:
You need to serialize your data into XML, and then transfer that in a
higher level protocol, such as HTTP. After you go with a pre-existing
system like those, they will answer all of your low-level questions, such
as how do I make this string arbitary length, or how do I add a new field,
or how do I change a field's meaning, or how to I interpret a transmission
error, etc.

Today, many people use XML over HTTP or even SOAP for the job
regardles if that is good or bad for the application.
The core ideea to keep in mind is the ammount of comunication and answer
time you want.
If these are not a issue (i.e. you send 2 msg/seccond and a 1/2...1 second
is ok) you can use XML over HTTP. If your client is going to exchange
hundreds or more msg/seccond with the server, and you want a very fast
response, then stay away of XML/HTTP.

About SOAP, my advice is use it only when is mandatory: i.e. when your
software have to comunicate with a given product who speak only SOAP.
Soap have a poor design (just look at Corba for comparation) and the
overhead is unbelivable: A Soap message with XML serialised data for the
struct of OP will waste about 20..50 times (yes, TIMES) as much bandwidth
and will waste about 10..100 times as much processing power on the server
side compared with when you serialize data by yourself in binary endian
independent format.
Jul 22 '05 #5
John Harrison wrote:
Write some code to convert the struct you want to send into a char array.
Write some code to turn that char array back into a struct. Use the first
piece of code when you send, the second when you recieve. Trying to send
anything more complicated than char arrays between different types of
computers is asking for trouble.


Oh, and then you must figure out either a length system for each part of the
string, or sentinels and delimeters for the ends of strings. Then you must
figure out how to escape the delimiters if your users type them inside
strings. Then you might need a system to name each data element.

Oh, and then you might localize, and need to pack UTF-8 into your strings.

Use XML. If HTTP is slow, even to a server you programmed, then rewrite a
simpler version of it.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #6
nospam <no****@example.com> wrote in message
news:_t********************@comcast.com...
Phlip wrote:
You need to serialize your data into XML, and then transfer that in a
higher level protocol, such as HTTP. After you go with a pre-existing
system like those, they will answer all of your low-level questions, such as how do I make this string arbitary length, or how do I add a new field, or how do I change a field's meaning, or how to I interpret a transmission error, etc.

Today, many people use XML over HTTP or even SOAP for the job
regardles if that is good or bad for the application.
The core ideea to keep in mind is the ammount of comunication and answer
time you want.
If these are not a issue (i.e. you send 2 msg/seccond and a 1/2...1 second
is ok) you can use XML over HTTP. If your client is going to exchange
hundreds or more msg/seccond with the server, and you want a very fast
response, then stay away of XML/HTTP.

Why XML/HTTP is slow?
About SOAP, my advice is use it only when is mandatory: i.e. when your
software have to comunicate with a given product who speak only SOAP.
Soap have a poor design (just look at Corba for comparation) and the
overhead is unbelivable: A Soap message with XML serialised data for the
struct of OP will waste about 20..50 times (yes, TIMES) as much bandwidth
and will waste about 10..100 times as much processing power on the server
side compared with when you serialize data by yourself in binary endian
independent format.

What consists overheads of SOAP?

Thanks for your insights in advance!
Jul 22 '05 #7

"Phlip" <ph*******@yahoo.com> wrote in message
news:qx*******************@newssvr19.news.prodigy. com...
John Harrison wrote:
Write some code to convert the struct you want to send into a char array.
Write some code to turn that char array back into a struct. Use the first
piece of code when you send, the second when you recieve. Trying to send
anything more complicated than char arrays between different types of
computers is asking for trouble.


Oh, and then you must figure out either a length system for each part of
the
string, or sentinels and delimeters for the ends of strings. Then you must
figure out how to escape the delimiters if your users type them inside
strings. Then you might need a system to name each data element.

Oh, and then you might localize, and need to pack UTF-8 into your strings.

Use XML. If HTTP is slow, even to a server you programmed, then rewrite a
simpler version of it.


Good advice but I got the impression that XML might be a bit beyond the OP.

john
Jul 22 '05 #8
away wrote:
Why XML/HTTP is slow?
on a standard x86 compiler
short n=htons(h);
will translate into 2 CPU instruction if optimisation is on

ostringstream str;
str<<"<val>"<<h<<"</val>";
string n=str.str();

will be at least hundreds if no more. It will require heap alocation etc...

in first example sizeof(s) will be 2
into the second s.size() will be at least 12 and at most 16.
If you add the <?xml version="1.0" ..... or namespaces
you got the point.

A dedicated TCP protocol implemented can send messages using only
a minimum overhead (like you can define your own message header with
one byte message type and a long the message size). Just compare the size
of a minimal http header with that. The http being stateless, you may need
to implement your own state keeping procedure if you need that. The
amount of the code executed inside of a application server prior to the
message reaching your handlers can be estimated very conservative to at
least thousands CPU instructions.
For most applications this is OK. If your request is going to make a query
into a database with milions of records, the overhead not significant
compared with the time spent in query. However, for some aplications
it may be unacceptable.

The point I made was NOT that you do not have to use XML. XML is very good
for most of the applications. But you can not state that this is the only
solution, as your answer:
"""
You need to serialize your data into XML, and then transfer that in a higher
level protocol, such as HTTP.
"""

without having a good insight of OP problem.

And by the way, there are over there some modern C++ socket libraries
and with a good implementation of message serialization, a custom socket
protocol may be way easier to implement that J2EE server side solution :-)
What consists overheads of SOAP?
Usually, SOAP just double the amount of data to be send
(serialized/send/deserialized) over a simple POST over HTTP.
Just look at the SOAP envelope required to send a single integer
as specified above :-)

Again, if your application do a query at every 2 seconds over a SQL
database, it do not mater. But at 100+ msg/s a SOAP implementation
will just collapse.

Thanks for your insights in advance!


You are welcome.

Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Perrin | last post by:
Hi, I am new to these forums so I don't know if this is the appropriate place to post it, but Iam really stuck and I need help :( I have this assignment with structs in C++. basically the...
3
by: Christian F | last post by:
Hi, I'm a C-newbie and I would like to know if I am doing something wrong in the code below. It is working, but I'm afraid it might not be correct because I don't really understand everything of...
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
5
by: Bilgehan.Balban | last post by:
Hi, I am currently brushing up my c++ knowledge and I would like to ask you about the differences between classes and C structs, in the function/method perspective. 1) Is it correct to say...
61
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
29
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much...
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
23
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
4
by: KioKrofov | last post by:
Currently I have (a much bigger version of) the code below in my main cpp file: struct myData { int dataCodeNum; char* dataName; } myData arrayOfData1={ { 1, ...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.