Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sending structure over network

Posted on 2002-05-10
9
Medium Priority
?
175 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
[X]
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
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 600 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

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.

Question has a verified solution.

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

618 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