Einzelnen Beitrag anzeigen

Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#43

AW: ClientSocket soll viele Server schnell abfragen

  Alt 24. Aug 2019, 08:57
Bei Verwendung von den ICS Komponenten werden keine Threads benötigt - die laufen asynchron und behindern sich damit nicht gegenseitig.

Mal ganz grob skizziert, wie der Test der Verbindungen aussehen könnte:

Delphi-Quellcode:
TNotifyConnectionResult = procedure(const AIp: string; const AErrorCode: Integer; const AInstance: TTestConnection) of object;

TTestConnection = class
private
   FClientSocket: TWSocket;
   FNotifyEvent: TNotifyConnectionResult;
   
   procedure OnClientSessionConnected(Sender: TObject; ErrCode: Word);
public
   procedure TestConnection(const AIp: string; const ANotifyEvent: TNotifyConnectionResult);
end;

TTestForm = class
private
   FListOfIpAddresses: TStringList;
   
   procedure NotifyConnectionResult(const AIp: string; const AErrorCode: Integer; const AInstance: TTestConnection);
   procedure TestAllConnections;
end;

implementation

procedure TTestConnection.TestConnection(const AIp: string; const ANotifyEvent: TNotifyConnectionResult);
begin
   FNotifyEvent := ANotifyEvent;
   FClientSocket.Addr := AIP;
   FClientSocket.Port := cDefaultPort; //oder Port mitgeben
   FClientSocket.OnSessionConnected := OnClientSessionConnected;
   FClientSocket.Connect;
end;

procedure TTestConnection.OnClientSessionConnected(Sender: TObject; ErrCode: Word);
begin
   FNotifyEvent(FClientSocket.Addr, ErrCode, Self); //Code 0: Verbunden, sonst Fehler
end;

//----

procedure TTestForm.TestAllConnections;
var
   IP: string;
   Test: TTestConnection;
begin
   for IP in FListOfIpAddresses do
   begin
      Test := TTestConnection.Create;
      Test.TestConnection(IP, NotifyConnectionResult);
   end;
end;

procedure TTestForm.NotifyConnectionResult(const AIp: string; const AErrorCode: Integer; const AInstance: TTestConnection);
begin
   if AErrorCode = 0 then
      //Erfolgreiche Verbindung anzeigen
   else
      //Verbindung fehlgeschlagen anzeigen
      
   AInstance.Free;
end;
Wo wird das Objekt "Test" wieder freigegeben?
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat