关于写多线程的Socket Client测试程序来测试Socket Server的问题(100分)

  • 主题发起人 主题发起人 coder
  • 开始时间 开始时间
C

coder

Unregistered / Unconfirmed
GUEST, unregistred user!
我的Server端程序完成后,需要写一个测试程序来做压力测试。但以前做的都是Server端的,
现在做了个Client端的却不正常。
我想同时启动20个Client端的线程,每个线程完成10次发送、接收后结束。
但只有第一个线程的第一次发送和接收是成功的。

想知道大家是如何写这种测试程序的,最好有个完整sample。

regards
 
你回应客户端的那段代码是怎么写的?
 
这是服务端线程的模型,数据包前4位记录包长度。
我怎么来给它作压力测试呢?

unit mySocketRW;

interface

uses
Classes, Sysutils, ScktComp, windows;

type
TDFServerThread = class(TServerClientThread)
private
FSocketStream : TWinSocketStream;
function WriteClient( strValue: String ): Boolean;
function ReadClient( var strValue: String; Count: integer): Boolean;
function DoTestClient: Boolean;
public
constructor Create( Suspend: Boolean; ASocket: TServerClientWinSocket );
procedure ClientExecute; override;
end;

implementation

uses Main;

procedure TDFServerThread.ClientExecute;
begin
FSocketStream := TWinSocketStream.Create( ClientSocket, 60000 );
try
while DoTestClient() do;
finally
FSocketStream.Free;
end;
end;

constructor TDFServerThread.Create(Suspend: Boolean;
ASocket: TServerClientWinSocket);
begin
inherited Create( Suspend, ASocket );
end;

function TDFServerThread.DoTestClient: Boolean;
var
i, iLen : integer;
sIn, sOut: string;
begin
result := false;
if ReadClient(sIn, 4) then
begin
iLen := strtointdef(trim(sIn),0);
if iLen < 0 then
begin
WriteLog('接收字符串长度错');
result:= false;
exit;
end;
if ReadClient(sIn, iLen) then
begin
sOut:= 'Handle:' + inttostr(self.Handle) + ' Now:'+ datetimetostr(now);
i := length(sOut);
sOut := copy(inttostr(10000+i),2,4) + sOut;
WriteClient(sOut);
WriteLog('接收=['+sIn+']'+' | '+'回应=['+sOut+']');
end
else
WriteLog('不能接收到长度为'+inttostr(iLen)+'的字符串');
end;
end;

function TDFServerThread.ReadClient(var strValue: String; Count: integer): Boolean;
var
buffer: array[0..254] of char;
nCount, nCut: integer;
begin
Assert(FSocketStream <> nil);
nCut:= Count;
try
while (not Terminated) and ClientSocket.Connected and (nCut>0) do
if FSocketStream.WaitForData( 2000 ) then
begin
nCount := FSocketStream.Read(buffer, Count);
if nCount > 0 then
begin
buffer[nCount] := #0;
strValue := strValue + pchar(string(buffer));
Dec(nCut, nCount);
end
else
break;
end
else
break;
except
Terminate;
end;
result := nCut = 0;
end;

function TDFServerThread.WriteClient( strValue: String ): Boolean;
var
buffer: array[0..400] of char;
nCount, nLeft : integer;
begin
Assert(FSocketStream <> nil);
nLeft := length(strValue);
try
repeat
if (Terminated or not ClientSocket.Connected ) then
break;
strmove(@buffer, pchar(strValue) , nLeft);
nCount := FSocketStream.Write( Buffer, nLeft );
Dec(nLeft, nCount);
Delete(strValue, 1, nCount );
until( (strValue='') or (nCount = 0) );
except
Terminate;
end;
result := nLeft = 0;
end;

end.
 
后退
顶部