| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- { $HDR$}
- {**********************************************************************}
- { Unit archived using Team Coherence }
- { Team Coherence is Copyright 2002 by Quality Software Components }
- { }
- { For further information / comments, visit our WEB site at }
- { http://www.TeamCoherence.com }
- {**********************************************************************}
- {}
- { $Log: 10363: IdTCPClient.pas
- {
- { Rev 1.0 2002.11.12 10:54:52 PM czhower
- }
- unit IdTCPClient;
- interface
- uses
- Classes, IdStack,
- IdException, IdGlobal, IdTCPConnection;
- const
- BoundPortDefault = 0;
- type
- TIdTCPClient = class(TIdTCPConnection)
- protected
- FBoundIP: string;
- FBoundPort: Integer;
- FBoundPortMax: Integer;
- FBoundPortMin: Integer;
- FHost: string;
- FOnConnected: TNotifyEvent;
- FPassword: string;
- FPort: integer;
- FUsername: string;
- //
- procedure SetHost(const Value: string); virtual;
- procedure SetPort(const Value: integer); virtual;
- procedure DoOnConnected; virtual;
- //
- property Username: string read FUsername write FUsername;
- property Password: string read FPassword write FPassword;
- public
- procedure Connect(const ATimeout: Integer = IdTimeoutDefault); virtual;
- function ConnectAndGetAll: string; virtual;
- constructor Create(AOwner: TComponent); override;
- destructor Destroy; override;
- //
- property BoundPortMax: Integer read FBoundPortMax write FBoundPortMax;
- property BoundPortMin: Integer read FBoundPortMin write FBoundPortMin;
- published
- property BoundIP: string read FBoundIP write FBoundIP;
- property BoundPort: Integer read FBoundPort write FBoundPort default BoundPortDefault;
- property Host: string read FHost write SetHost;
- property OnConnected: TNotifyEvent read FOnConnected write FOnConnected;
- property Port: integer read FPort write SetPort;
- end;
- implementation
- uses
- IdComponent, IdIOHandlerSocket, IdResourceStrings,
- SysUtils;
- { TIdTCPClient }
- procedure TIdTCPClient.Connect(const ATimeout: Integer = IdTimeoutDefault);
- begin
- // Do not call Connected here, it will call CheckDisconnect
- if IOHandler <> nil then begin
- if IOHandler.Connected then begin
- raise EIdAlreadyConnected.Create(RSAlreadyConnected);
- end;
- end else begin
- IOHandler := TIdIOHandlerSocket.Create(Self);
- IOHandler.OnStatus := OnStatus;
- FFreeIOHandlerOnDisconnect := True;
- end;
- try
- IOHandler.Open;
- ResetConnection;
- // Socks support
- IOHandler.ConnectClient(Host, Port, BoundIP, BoundPort, BoundPortMin, BoundPortMax, ATimeout);
- if Assigned(Intercept) then begin
- Intercept.Connect(Self);
- end;
- DoStatus(hsConnected, [Host]);
- DoOnConnected;
- except
- // This will free IOHandler
- DisconnectSocket;
- raise;
- end;
- end;
- function TIdTCPClient.ConnectAndGetAll: string;
- begin
- Connect; try
- Result := AllData;
- finally Disconnect; end;
- end;
- constructor TIdTCPClient.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FBoundPort := BoundPortDefault;
- end;
- destructor TIdTCPClient.Destroy;
- begin
- inherited Destroy;
- end;
- procedure TIdTCPClient.DoOnConnected;
- begin
- if Assigned(OnConnected) then begin
- OnConnected(Self);
- end;
- end;
- procedure TIdTCPClient.SetHost(const Value: string);
- begin
- FHost := Value;
- end;
- procedure TIdTCPClient.SetPort(const Value: integer);
- begin
- FPort := Value;
- end;
- end.
|