?
Solved

Sending Record Data Structure in TCP C/S Application

Posted on 2001-08-06
3
Medium Priority
?
168 Views
Last Modified: 2010-04-06
Could someone give me a complete example of
sending the following record structure back and forth between Server & client utilizing the TServerSocket & TClientSocket components.

Type
 
 TOperationType = (ctAdd, ctDelete, ctModify);
 
 TMyRecord
  StringField: String;
  BoolField: Boolean;
  IntegerField: Integer;
  FloatField: Real;
  DateField: TDateTime;
  MemoFiled: TStrings;
  OperationField: TCustomType;
 end;

I will use the OperationField to determine what to do with the record on the appropriate side (Server/Client).

Shane
0
Comment
Question by:shaneholmes
[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
3 Comments
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6357801
You could try this:

var
  MyRecord: TMyRecord;
begin
  // do something with MyRecord, fill it with data, etc
  MyServerSocket.SendBuf(MyRecord, SizeOf(TMyRecord));
end;

for the server socket, write a handler for OnClientRead and use ReceiveBuf to retrieve the record. Similarly, for the client socket, write a handler for OnRead



DragonSlayer.
0
 
LVL 9

Accepted Solution

by:
ITugay earned 400 total points
ID: 6364078
Hi shaneholmes,

it seems you have to create a stream and write all fields of your record one by one:

procedure TForm1.SendRecord(T: TMyRecord);
var
  S: TMemoryStream;
  W: TWriter;
begin
  S := TMemoryStream.Create;

  // create helper object to make your life easy
  W := TWriter.Create(S, 1024);

  // preserve space in stream for total stream size
  W.WriteInteger(0);

  // write record to the stream
  W.WriteString(T.StringField);
  W.WriteBoolean(T.BoolField);
  W.WriteDate(T.DateField);
  //...
  T.MemoFiled.SaveToStream(S);
  W.WriteInteger(Integer(T.OperationField));

  // set amount of bytes to send
  // this value will be used in server part
  S.Seek(0, soFromBeginning);
  W.WriteInteger(S.Size);

  try
    if not ClientSocket1.Socket.SendStream(S) then
      raise Exception.Create('error while sending data');
  finally
    // be sure that temporary objects are freed
    W.Free;
    S.Free;
  end;
end;


procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  S: TMemoryStream;
  R: TReader;
  N: Integer;
begin

  // T: TMyRecord;

  S := TMemoryStream.Create;
  R := TReader.Create(S, 1024);

  // get amount of bytes to read
  Socket.ReceiveBuf(N, SizeOf(Integer));
  // wet stream size
  S.SetSize(N-SizeOf(Integer));
  // read data from socket
  Socket.ReceiveBuf(S.Memory^, S.Size);

  // read fields to record
  T.StringField := R.ReadString;
  T.BoolField := R.ReadBoolean;
  T.DateField := R.ReadDate;
  //...
  T.MemoFiled.LoadFromStream(S);
  T.OperationField := TOperationType(R.ReadInteger);

  R.Free;
  S.Free;
end;

------
Igor
0
 
LVL 11

Author Comment

by:shaneholmes
ID: 6375077
Great Job ITugay, I can work with this!

Thanks!

Shane
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

762 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