Solved

How to use records with TIdTCPClient in Delphi

Posted on 2009-06-29
2
1,399 Views
Last Modified: 2013-11-13
Hi,

I'm using Delphi 7 and Indy 9 to send data to a Server. In my code, I use a record that I initialize first and then pass it to the WriteBuffer of WriteStream. Most of the record fields are sent normally except for the string field that seems to send garbage instead of the characters I initialized it with.

Anyone can help me with that? or Is there an example somewhere so that I can base my ork on?

Thanks
unit frmClient;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, ExtCtrls, IdAntiFreezeBase, IdAntiFreeze;
 
type
  TProcolFrameSend = packed record
    iLength: Integer;
    iType: Integer;
    iID: Integer;
    iControl: Integer;
    iNbrSeries: Integer;
    iNbrParams: Integer;
    iParamLength: Integer;
    sParamValue: string;
  end;
 
  TForm1 = class(TForm)
  // objets and procedures deleted to simplify code...
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.cmdConnectClick(Sender: TObject);
  var
    imgStream : TMemoryStream;
    longTrame : integer;
begin
  If tcpClientSkt.Connected Then
  begin
    mStatus.Lines.Add('Already connected to server.');
    Exit;
  end;
  mStatus.Clear;
  mStatus.Lines.Add('Connecting to server');
  try
    tcpClientSkt.Host := 'CMV-10001-11019';
    tcpClientSkt.Port := 5001;
    tcpClientSkt.Connect(5000);
    mStatus.Lines.Add(tcpClientSkt.ReadLn());
  except
    on E: Exception do MessageDlg ('Error connecting to server'+#13+E.Message, mtError, [mbOk], 0);
  end;
end;
 
procedure TForm1.tcpClientSktConnected(Sender: TObject);
begin
  mStatus.Lines.Add('Conneted to server ' + tcpClientSkt.Host);
end;
 
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  If tcpClientSkt.Connected Then
  begin
    mStatus.Lines.Add('Deconnecting from server');
    tcpClientSkt.Disconnect();
  end
end;
 
procedure TForm1.cmdDisconnectClick(Sender: TObject);
begin
  If tcpClientSkt.Connected Then
  begin
    mStatus.Clear;
    mStatus.Lines.Add('Deconnecting from server');
    tcpClientSkt.Disconnect();
  end
end;
 
procedure TForm1.tcpClientSktDisconnected(Sender: TObject);
begin
  mStatus.Lines.Add('Disconnected from server');
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  mStatus.Clear;
end;
 
procedure TForm1.Button2Click(Sender: TObject);
var
  FrameSend: TProcolFrameSend;
  inBuf : TMemoryStream;
  i : Integer;
  tempStr : AnsiString;
begin
 
  // Control Info
  FrameSend.iType := 30104;
  FrameSend.iID := 0;
  FrameSend.iControl := 0;
  FrameSend.iNbrSeries := 1;
  FrameSend.iNbrParams := 2;
 
  // param length
  FrameSend.iParamLength := StrLen(PChar(ListBox1.Items[ListBox1.ItemIndex]));
  // Frame total lenght
  FrameSend.iLength := 28 + FrameSend.iParamLength;
 
  // Parameter value, the name of a file to be sent by the server
  FrameSend.sParamValue := ListBox1.Items[ListBox1.ItemIndex];
 
  If tcpClientSkt.Connected Then
  begin
    try
 
      tcpClientSkt.OpenWriteBuffer(-1);
      tcpClientSkt.SendBufferSize := FrameSend.iLength;
      tcpClientSkt.WriteBuffer(FrameSend, FrameSend.iLength, true);
      tcpClientSkt.CloseWriteBuffer();
 
      inBuf := TMemoryStream.Create;
      tcpClientSkt.ReadStream(inBuf);
      inBuf.Position := 0;
 
      mStatus.Lines.Add('Document received');
 
    finally
      if tcpClientSkt.Connected then inBuf.Free;
    end;
  end;
end;
 
end.

Open in new window

0
Comment
Question by:pmaltais
2 Comments
 
LVL 18

Accepted Solution

by:
Johnjces earned 500 total points
ID: 24736912
If you download the Indy demos, there is a sample application called SendReceiveRecords as I recall, that sends binary images.

Might be exactly what you need to base your work form.

http://www.indyproject.org/demos/index.html

John
0
 

Author Closing Comment

by:pmaltais
ID: 31597934
Thanks. The demos really helped.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi 2 60
Working with hours 3 58
Installshield for Embarcadero EX 10.1 Berlin 4 41
can i put an image from resource into image tag ? 6 25
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

773 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