Link to home
Start Free TrialLog in
Avatar of bnz
bnz

asked on

Sending structure over network

Hi

I have some structures which I want to send to another computer on my network.
The structure includes strings and ordinal types.

Right now I'm inheriting a TComponent stores my data as published properties and then stream the component to a string and at the other side I convert the string to a component object. This works fine, but seems a bit dumb.

So any ideas how do it in a cleaner way.

I thought of using records but this is AFAIK not possible because of the strings. I want a generic procedure to stream my structure to data and then back again.
Avatar of Lee_Nover
Lee_Nover

well you should never use any non-fixed-length strings in a record
hum..
if you still prefer records then make some "Header + data" protocol
you could make something like :

type
  TmyRec = packed record
    Param1,
    Param2: Integer;
    StringLength: Integer;
    TheString: string;
  end;

var myRec: TMyRec;


I don't know what you're using to write over network ...
(mailslots ?)so I'll just make some dummy procedure


procedure SendMyRec(rec: TMyRec);
begin
  SendData(rec, SizeOf(rec));
  if rec.StringSize > 0 then
    SendData(rec.TheString[1], rec.StringSize);
end;

procedure GetMyRec(var rec: TMyRec);
begin
  GetData(rec, SizeOf(rec));
  if rec.StringSize > 0 then
  begin
    SetLength(rec.TheString, rec.StrngSize);
    GetData(rec.TheString[1], rec.StringSize);
  end;
end;

just an idea of how to do it :)
ASKER CERTIFIED SOLUTION
Avatar of nestorua
nestorua

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

This is a good excuse to try some XML and put it on your resume. :)  Use XML to package your data and conform to an industry standard!  It is a very extendible format so you will find it easier in the long run.

Any web search should come up with lots of XML sites.

Basically your data is a simple string might look like (you can remove the linebreaks and spurious spacing):

<mydata>
 <record><name>Fred</name><age>101</age></record>
 <record><name>Joe</name><age>20</age></record>
...
 <record><name>Sue</name><age>55</age></record>
</mydata>

Beats csv or fixed format as they don't allow nesting of data.  See how XML is build around nesting.  But, like other data formats, expressing links might be an issue...

You have to be careful to 'encode' any XML-special characters though.

You could override the TPersistent methods as described by Nestorua to do the XML too.

Cheers,

-Mat
...just continuing from my post above:
Btw, there are several good Delphi free libraries around which will do the work of reading and writing to XML from a nested object structure (and worrying about any special encoding).

Cheers,
-Mat
Good comments here!  ;-)
Avatar of bnz

ASKER

Sorry for not posting any comments before. I didn't receive any notification mail from E-E,  probally my email problem.

I don't want to play around with XML, I think I want to go with nestoruas way. I will return when I run into trouble or have the solution made.

BTW Don't lock the question with an answer unless you are 100% sure that you posted what I needed.
Avatar of bnz

ASKER

Sorry for not posting any comments before. I didn't receive any notification mail from E-E,  probally my email problem.

I don't want to play around with XML, I think I want to go with nestoruas way. I will return when I run into trouble or have the solution made.

BTW Don't lock the question with an answer unless you are 100% sure that you posted what I needed.
listening
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept answer from nestorua

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Paul (pnh73)
EE Cleanup Volunteer