Solved

Sending structure over network

Posted on 2002-05-10
9
164 Views
Last Modified: 2010-04-04
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.
0
Comment
Question by:bnz
9 Comments
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7002491
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 :)
0
 
LVL 4

Accepted Solution

by:
nestorua earned 150 total points
ID: 7003081
HI, bnz,
I propose the following plan of solving your problem:
1.Create the TPersistent object of your structure.
2.Create the methods LoadFromStream, SaveToStream,
LoadFromFile, SaveToFile using TReader and TWriter and
DefineProperty (it also be useful if you would want to write
this structure to DELPHI stream).
3.Using SendStream, SendBuf, ReceiveBuf of the TClientSocket and TServerSocket components you can send and receive your
data over the network.
Sincerely,
Nestorua.
0
 

Expert Comment

by:mfhobbs
ID: 7003839
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
0
 

Expert Comment

by:mfhobbs
ID: 7003844
...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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:SteveWaite
ID: 7003859
Good comments here!  ;-)
0
 

Author Comment

by:bnz
ID: 7004419
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.
0
 

Author Comment

by:bnz
ID: 7004421
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.
0
 
LVL 3

Expert Comment

by:smurff
ID: 7004424
listening
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9005583
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to convert wav to mp3 in delphi 9 174
delphi prevent click fast 2 191
Adoquery sql  left join does not work 25 80
Base1 Encode/Decode 3 68
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now