客户端测试代码:
unit ConnMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Sockets;
type
TThreadConnections = class(TThread)
private
FRemoteAddress: string;
FRemotePort: string;
FClientSocket: TTcpClient;
protected
procedure Execute; override;
procedure AddResult;
public
iCount: integer;
FRecvStr: string;
constructor Create(CreateSuspended: Boolean; Address, Port: string);
destructor Destroy; override;
end;
TForm1 = class(TForm)
Label3: TLabel;
edtThreads: TEdit;
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Label1: TLabel;
edtAddress: TEdit;
Label2: TLabel;
edtPort: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
ThreadPools: TList;
Conn: TThreadConnections;
public
{ Public declarations }
procedure AddResult(RecvStr: string);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
constructor TThreadConnections.Create(CreateSuspended: Boolean; Address, Port: string);
begin
FreeOnTerminate := True;
FRemoteAddress := Address;
FRemotePort := Port;
iCount := 0;
inherited Create(CreateSuspended);
end;
destructor TThreadConnections.Destroy;
begin
inherited Destroy;
end;
procedure TThreadConnections.Execute;
begin
try
while not Terminated do
begin
if not Assigned(FClientSocket) then
begin
FClientSocket := TTcpClient.Create(nil);
FClientSocket.RemotePort := FRemotePort;
FClientSocket.RemoteHost := FRemoteAddress;
FClientSocket.Open;
FRecvStr := FClientSocket.Receiveln;
Synchronize(AddResult);
Inc(iCount);
end else
if FClientSocket.Connected then
begin
FClientSocket.Sendln(IntToStr(ThreadID) + ' : Open:' + IntToStr(iCount));
FRecvStr := FClientSocket.Receiveln;
Synchronize(AddResult);
if FClientSocket <> nil then
FClientSocket.Close;
FreeAndNil(FClientSocket);
sleep(40);
end
end;
except
raise ;
end;
end;
procedure TThreadConnections.AddResult;
begin
Form1.AddResult(FRecvStr);
end;
procedure TForm1.AddResult(RecvStr: string);
begin
Memo1.Lines.Add(RecvStr);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i, iThreads: integer;
Address, Port: string;
begin
iThreads := StrToInt(edtThreads.Text);
Address := Trim(edtAddress.Text);
Port := Trim(edtPort.Text);
for i := 1 to iThreads do
begin
Conn := TThreadConnections.Create(True, Address, Port);
ThreadPools.Add(Conn);
end;
for i := 0 to ThreadPools.Count - 1 do
TThreadConnections(ThreadPools).Resume;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
try
while ThreadPools.Count > 0 do
begin
if Assigned(ThreadPools[0]) then
begin
if TThreadConnections(ThreadPools[0]).Suspended then
TThreadConnections(ThreadPools[0]).Resume;
Memo1.Lines.Add(IntToStr(TThreadConnections(ThreadPools[0]).ThreadID) +
' : ' + IntToStr(TThreadConnections(ThreadPools[0]).iCount));
TThreadConnections(ThreadPools[0]).Terminate;
end;
ThreadPools.Delete(0);
end;
except
ThreadPools.Clear;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ThreadPools := TList.Create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeAndNil(ThreadPools);
end;
end.