试试这个
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, ExtCtrls;
type
TForm1 = class(TForm)
IdTCPClient1: TIdTCPClient;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Memo1: TMemo;
Edit1: TEdit;
Label1: TLabel;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure IdTCPClient1Connected(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure IdTCPClient1Disconnected(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button3Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
// type
// PSocketThread=^TSocketThread;
// TSocketThread=Record
// SocketThread:TIdPeerThread;
// Next
SocketThread;
// THID:Integer;
// end;
var
Form1: TForm1;
td
word;
doRead:boolean;
implementation
{$R *.dfm}
procedure ReadThread;
var
s:String;
begin
form1.Memo1.Lines.Add('Begin reading...');
Form1.Label1.Caption:=IntToStr(td);
s:=Form1.IdTCPClient1.ReadLn;
while doRead do
begin
s:=Form1.IdTCPClient1.ReadLn;
form1.Memo1.Lines.Add(s);
sleep(100);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
IdTCPClient1.Connect(1000);
doRead:=true;
CreateThread(nil,0,@ReadThread,nil,0,td);
end;
procedure TForm1.IdTCPClient1Connected(Sender: TObject);
begin
Memo1.Lines.Clear;
Memo1.Lines.Add('connected to server');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
IdTCPClient1.WriteLn(Edit1.Text);
end;
procedure TForm1.IdTCPClient1Disconnected(Sender: TObject);
begin
ExitThread(td);
Memo1.Lines.Add('disConnected from server');
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
doRead:=false;
IdTCPClient1.Disconnect;
ExitThread(td);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
doRead:=false;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Self.Label1.Caption:=IntToStr(td);
end;
end.