E
eachbuilder
Unregistered / Unconfirmed
GUEST, unregistred user!
多服务器端(20个),单客户端多线程与它们通讯,每个线程连接一个服务器端,线程如何处理收发数据包,现用ICS的WSocket
TClientThread = class(TThread)
private
FWSocket: TWSocket;
FRcvBuf : string;
procedure FWSocketSessionConnected(Sender: TObject; Error: Word);
procedure FWSocketSessionClosed(Sender: TObject; Error: Word);
procedure FWSocketDataAvailable(Sender: TObject; Error: Word);
protected
procedure Execute; override;
public
constructor Create();
destructor Destroy; override;
end;
constructor TClientThread.Create;
begin
FreeOnTerminate:= True;
inherited Create(False);
end;
procedure TClientThread.Execute;
begin
FWSocket := TWSocket.Create(nil);
FWSocket.MultiThreaded := True;
FWSocket.Proto := 'tcp';
FWSocket.Port := '6706';
FWSocket.Addr := 'localhost';
FWSocket.LineMode := True;
FWSocket.LineEnd := #13#10;
FWSocket.OnSessionConnected := FWSocketSessionConnected;
FWSocket.OnSessionClosed := FWSocketSessionClosed;
FWSocket.OnDataAvailable := FWSocketDataAvailable;
FWSocket.Connect;
FWSocket.MessageLoop;
FWSocket.Destroy;
end;
procedure TClientThread.FWSocketSessionConnected(Sender: TObject; Error: Word);
begin
FWSocket.SendStr('Hello' + #13 + #10);
end;
procedure TClientThread.FWSocketSessionClosed(Sender: TObject; Error: Word);
begin
FWSocket.Release;
FWSocket:= nil;
end;
procedure TClientThread.FWSocketDataAvailable(Sender: TObject; Error: Word);
var
Buffer: array [0..4095] of Char;
Count : Integer;
begin
Count:= FWSocket.Receive(@Buffer, SizeOf(Buffer));
if (Count > 0) then
begin
if Buffer[0] = 'K' then
begin
FWSocket.SendStr('OK' + #13 + #10);
end
else begin
FWSocket.Send(@Buffer, Count);
end;
... //还有好多命令
end;
end;
destructor TClientThread.Destroy;
begin
if Assigned(FWSocket) then FWSocket.Close;
inherited;
end;
因为我是客户端,我要在主线程(MainFrm)里主动发送请求数据,然后服务器端响应,返回数据。
可是怎么发呢?还有,我现在是开一个线程连接一个服务器端,如何区分不同的服务器端的发送和接收?
TClientThread = class(TThread)
private
FWSocket: TWSocket;
FRcvBuf : string;
procedure FWSocketSessionConnected(Sender: TObject; Error: Word);
procedure FWSocketSessionClosed(Sender: TObject; Error: Word);
procedure FWSocketDataAvailable(Sender: TObject; Error: Word);
protected
procedure Execute; override;
public
constructor Create();
destructor Destroy; override;
end;
constructor TClientThread.Create;
begin
FreeOnTerminate:= True;
inherited Create(False);
end;
procedure TClientThread.Execute;
begin
FWSocket := TWSocket.Create(nil);
FWSocket.MultiThreaded := True;
FWSocket.Proto := 'tcp';
FWSocket.Port := '6706';
FWSocket.Addr := 'localhost';
FWSocket.LineMode := True;
FWSocket.LineEnd := #13#10;
FWSocket.OnSessionConnected := FWSocketSessionConnected;
FWSocket.OnSessionClosed := FWSocketSessionClosed;
FWSocket.OnDataAvailable := FWSocketDataAvailable;
FWSocket.Connect;
FWSocket.MessageLoop;
FWSocket.Destroy;
end;
procedure TClientThread.FWSocketSessionConnected(Sender: TObject; Error: Word);
begin
FWSocket.SendStr('Hello' + #13 + #10);
end;
procedure TClientThread.FWSocketSessionClosed(Sender: TObject; Error: Word);
begin
FWSocket.Release;
FWSocket:= nil;
end;
procedure TClientThread.FWSocketDataAvailable(Sender: TObject; Error: Word);
var
Buffer: array [0..4095] of Char;
Count : Integer;
begin
Count:= FWSocket.Receive(@Buffer, SizeOf(Buffer));
if (Count > 0) then
begin
if Buffer[0] = 'K' then
begin
FWSocket.SendStr('OK' + #13 + #10);
end
else begin
FWSocket.Send(@Buffer, Count);
end;
... //还有好多命令
end;
end;
destructor TClientThread.Destroy;
begin
if Assigned(FWSocket) then FWSocket.Close;
inherited;
end;
因为我是客户端,我要在主线程(MainFrm)里主动发送请求数据,然后服务器端响应,返回数据。
可是怎么发呢?还有,我现在是开一个线程连接一个服务器端,如何区分不同的服务器端的发送和接收?