Solved

Sending Record Data Structure in TCP C/S Application

Posted on 2001-08-06
3
153 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
3 Comments
 
LVL 14

Expert Comment

by:DragonSlayer
Comment Utility
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 100 total points
Comment Utility
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
Comment Utility
Great Job ITugay, I can work with this!

Thanks!

Shane
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

13 Experts available now in Live!

Get 1:1 Help Now