🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #1118

Re: Serializing struct for TCP sending

#1118
From: Remy Lebeau (TeamB)
Date: Tue, 15 May 2007 20:33
60 lines
2873 bytes
"Stefano Bonifazi" <stefboombastic@email.it> wrote in message
news:464a101b@newsgroups.borland.com...

> I always used Indy SendBuffer and on the other side even
> from Linux they receive fine!

Windows and Linux run on the same hardware architecture, so the endian
is the same.  But if you were to exchange data with to a Mac or other
non-PC machine, the endian would likely not match.  Read/WriteBuffer()
do not perform any endian conversions at all.
Read/WriteInteger/Cardinal/SmallInt(), on the other hand, do perform
conversions from/to network byte ordering by default.  All systems
understand network byte ordering, and the socket API provides
functions (which Indy wraps internally) to convert numbers between
network byte ordering (which is universal across networks) and host
byte ordering (which is specific to the machine being run on).  So,
what you could do is send the individual members of your struct
separately, ie:

    my_struct s;

    TCPClient->WriteBuffer(s.name, 255);
    TCPClient->WriteInteger(s.intnum);
    // and so on...

    TCPClient->ReadBuffer(s.name, 255);
    s.intnum = TCPClient->ReadInteger();
    // and so on...

Also, you might want to stay away from floating point numbers, if you
can.  The byte size and binary representation may work differently on
different hardware architectures as well, regardless of the endian
used.

> U mean in "advanced compiler" options, "data alignment" right?

No.  That is a global setting that effects the entire application as a
whole.  That would be very dangerous and error-prone.  What you should
do instead is wrap your individual structs with #pragma pack
statements instead, ie:

    #pragma pack(push, 1)
    struct my_struct
    {
        //...
    };
    #pragma pack(pop)

Or:

    #include <pshpack1.h>
    struct my_struct
    {
        //...
    };
    #include <poppack.h>


Gambit
Technical Details
Message-ID: <464a1abf@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <4649f789@newsgroups.borland.com> <hevj4394fkkbve5uiuicaf1614234a44j3@4ax.com> <464a0555@newsgroups.borland.com> <464a101b@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,fd5a9df59ed83942
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <4649f789@newsgroups.borland.com> <hevj4394fkkbve5uiuicaf1614234a44j3@4ax.com> <464a0555@newsgroups.borland.com> <464a101b@newsgroups.borland.com>
Subject: Re: Serializing struct for TCP sending
Date: Tue, 15 May 2007 13:33:24 -0700
Lines: 62
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 75.82.60.244
Message-ID: <464a1abf@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1179261631 75.82.60.244 (15 May 2007 13:40:31 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail