Solved

Sending structure over network

Posted on 2002-05-10
9
162 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 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

19 Experts available now in Live!

Get 1:1 Help Now