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;
} MsgCoreInfoStru ct;
And then I use something like this:
MsgCoreInfoStru ct myData;
strcpy(myData.v ersionString, "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 MsgCoreInfoStru ct* 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 8 2140
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
"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; } MsgCoreInfoStru ct;
And then I use something like this:
MsgCoreInfoStru ct myData; strcpy(myData.v ersionString, "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 MsgCoreInfoStru ct* 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
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 "deserializ e" 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.
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.
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
nospam <no****@example .com> wrote in message
news:_t******** ************@co mcast.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!
"Phlip" <ph*******@yaho o.com> wrote in message
news:qx******** ***********@new ssvr19.news.pro digy.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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 following
situation
its an administration system
main function: opens a file and sends it to a boolean function that
uses the struct declaration (name, lastname, birthdate etc) to read
the file and give a cout of the found data. The boolean itself...
|
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 it. There are
lots of pointers and pointers to pointers which makes me confused.
First my typedef:
typedef struct
{
double re;
|
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;
|
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 that, a structure definition that includes
function pointers only defines the function prototypes to be used with
them, but not the actual implementations, whereas in C++, member
functions cannot be changed *unless* virtual functions are used, or the
|
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 (myBranch + x) = new Branch(i); // it doesn't
x is a loop iterator, i is an int for the constructor to define an
array.
What am I doing wrong here.
| |
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 overhead (I
think). A Struct seems more appropriate. At least it is what I would
have used in other languages. But since a Struct *can* hold methods,
I wander if I am saving anything. If not, why use it?
|
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 "there is no difference between a struct
and a class except the public/private access specification" (and a few minor
other things). When I create a class, I always start by declaring the
default constructor, copy constructor and assignment operator...
|
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;
};
|
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, "Data 1.1" },
|
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,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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...
|
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...
|
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...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |