Einzelnen Beitrag anzeigen

Benutzerbild von Stevie
Stevie

Registriert seit: 12. Aug 2003
Ort: Soest
4.016 Beiträge
 
Delphi 10.1 Berlin Enterprise
 
#16

Re: Dialog mit einem Server über Indy TCP

  Alt 28. Jun 2004, 13:23
Hab mal kurz was zusammengefrickelt...
Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, StdCtrls;

type
  TForm1 = class(TForm)
    IdTCPClient: TIdTCPClient;
    lbCommunication: TListBox;
    btnConnect: TButton;
    btnDisconnect: TButton;
    edtHost: TEdit;
    edtPort: TEdit;
    Memo1: TMemo;
    Timer: TTimer;
    procedure btnConnectClick(Sender: TObject);
    procedure IdTCPClientConnected(Sender: TObject);
    procedure IdTCPClientDisconnected(Sender: TObject);
    procedure TimerTimer(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnConnectClick(Sender: TObject);
begin
  lbCommunication.Items.Clear;
  with IdTCPClient do
    begin
      Host := edtHost.Text;
      Port := StrToint(edtPort.Text);
      try
        Connect; // add a timeout here if you wish, eg: Connect(3000) = timeout after 3 seconds.
        WriteLn('GET / HTTP/1.0'+#13#10);
      except
        on E : Exception do
// LockControls(True);
          ShowMessage(E.Message);
      end;
    end;
end;

procedure TForm1.IdTCPClientConnected(Sender: TObject);
begin
  btnConnect.Enabled := false;
  btnDisconnect.Enabled := true;
  Timer.Enabled := True;
end;

procedure TForm1.IdTCPClientDisconnected(Sender: TObject);
begin
  btnConnect.Enabled := true;
  btnDisconnect.Enabled := false;
  Timer.Enabled := False;
end;

procedure TForm1.TimerTimer(Sender: TObject);
var
  s: string;
begin
  s := IdTCPClient.ReadLn(#$A, 5);
  if s <> 'then
    Memo1.Lines.Add(s);
end;

end.
Stefan
“Simplicity, carried to the extreme, becomes elegance.” Jon Franklin

Delphi Sorcery - DSharp - Spring4D - TestInsight
  Mit Zitat antworten Zitat