unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ScktComp, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient;
type
TForm1 =
class(TForm)
Panel1: TPanel;
Panel2: TPanel;
Panel3: TPanel;
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Edit2: TEdit;
Memo1: TMemo;
Edit3: TEdit;
TCP: TIdTCPClient;
procedure Edit3KeyPress(Sender: TObject;
var Key: Char);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
procedure SendLine(Src:
String);
end;
TClientHandleThread =
class(TThread)
private
Message:
String;
procedure HandleInput;
protected
procedure Execute;
override;
end;
var
Form1: TForm1;
ClientHandleThread: TClientHandleThread;
implementation
{$R *.dfm}
procedure TForm1.Edit3KeyPress(Sender: TObject;
var Key: Char);
begin
if (key = #13)
then begin
memo1.lines.add('
OUT: ' + edit3.Text);
SendLine(edit3.text);
edit3.clear;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
tcp.Disconnect;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if tcp.Connected
then begin
memo1.lines.add('
already connected');
exit;
end;
TCP.Host := edit1.Text;
TCP.Port := strtoint(edit2.Text);
try
TCP.Connect(3000);
ClientHandleThread := TClientHandleThread.Create(True);
ClientHandleThread.FreeOnTerminate:=True;
ClientHandleThread.Resume;
except
showmessage('
error!');
end;
end;
procedure TForm1.SendLine(Src:
String);
begin
tcp.WriteInteger(length(Src));
tcp.
Write(src);
end;
procedure TClientHandleThread.HandleInput;
begin
form1.memo1.lines.add('
IN: ' +
Message);
end;
procedure TClientHandleThread.Execute;
begin
while not Terminated
do
begin
if not Form1.TCP.Connected
then
Terminate
else
try
Message := Form1.TCP.ReadString(Form1.TCP.ReadInteger);
Synchronize(HandleInput);
except
end;
end;
end;
end.