Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Sending structure over network

Posted on 2002-05-10
9
170 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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