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.