Solved

Sending Record Data Structure in TCP C/S Application

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

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…
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…

733 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