Solved

Sending Record Data Structure in TCP C/S Application

Posted on 2001-08-06
3
167 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 100 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

Independent Software Vendors: 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

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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

707 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