IdTCPClient.pas 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. { $HDR$}
  2. {**********************************************************************}
  3. { Unit archived using Team Coherence }
  4. { Team Coherence is Copyright 2002 by Quality Software Components }
  5. { }
  6. { For further information / comments, visit our WEB site at }
  7. { http://www.TeamCoherence.com }
  8. {**********************************************************************}
  9. {}
  10. { $Log: 10363: IdTCPClient.pas
  11. {
  12. { Rev 1.0 2002.11.12 10:54:52 PM czhower
  13. }
  14. unit IdTCPClient;
  15. interface
  16. uses
  17. Classes, IdStack,
  18. IdException, IdGlobal, IdTCPConnection;
  19. const
  20. BoundPortDefault = 0;
  21. type
  22. TIdTCPClient = class(TIdTCPConnection)
  23. protected
  24. FBoundIP: string;
  25. FBoundPort: Integer;
  26. FBoundPortMax: Integer;
  27. FBoundPortMin: Integer;
  28. FHost: string;
  29. FOnConnected: TNotifyEvent;
  30. FPassword: string;
  31. FPort: integer;
  32. FUsername: string;
  33. //
  34. procedure SetHost(const Value: string); virtual;
  35. procedure SetPort(const Value: integer); virtual;
  36. procedure DoOnConnected; virtual;
  37. //
  38. property Username: string read FUsername write FUsername;
  39. property Password: string read FPassword write FPassword;
  40. public
  41. procedure Connect(const ATimeout: Integer = IdTimeoutDefault); virtual;
  42. function ConnectAndGetAll: string; virtual;
  43. constructor Create(AOwner: TComponent); override;
  44. destructor Destroy; override;
  45. //
  46. property BoundPortMax: Integer read FBoundPortMax write FBoundPortMax;
  47. property BoundPortMin: Integer read FBoundPortMin write FBoundPortMin;
  48. published
  49. property BoundIP: string read FBoundIP write FBoundIP;
  50. property BoundPort: Integer read FBoundPort write FBoundPort default BoundPortDefault;
  51. property Host: string read FHost write SetHost;
  52. property OnConnected: TNotifyEvent read FOnConnected write FOnConnected;
  53. property Port: integer read FPort write SetPort;
  54. end;
  55. implementation
  56. uses
  57. IdComponent, IdIOHandlerSocket, IdResourceStrings,
  58. SysUtils;
  59. { TIdTCPClient }
  60. procedure TIdTCPClient.Connect(const ATimeout: Integer = IdTimeoutDefault);
  61. begin
  62. // Do not call Connected here, it will call CheckDisconnect
  63. if IOHandler <> nil then begin
  64. if IOHandler.Connected then begin
  65. raise EIdAlreadyConnected.Create(RSAlreadyConnected);
  66. end;
  67. end else begin
  68. IOHandler := TIdIOHandlerSocket.Create(Self);
  69. IOHandler.OnStatus := OnStatus;
  70. FFreeIOHandlerOnDisconnect := True;
  71. end;
  72. try
  73. IOHandler.Open;
  74. ResetConnection;
  75. // Socks support
  76. IOHandler.ConnectClient(Host, Port, BoundIP, BoundPort, BoundPortMin, BoundPortMax, ATimeout);
  77. if Assigned(Intercept) then begin
  78. Intercept.Connect(Self);
  79. end;
  80. DoStatus(hsConnected, [Host]);
  81. DoOnConnected;
  82. except
  83. // This will free IOHandler
  84. DisconnectSocket;
  85. raise;
  86. end;
  87. end;
  88. function TIdTCPClient.ConnectAndGetAll: string;
  89. begin
  90. Connect; try
  91. Result := AllData;
  92. finally Disconnect; end;
  93. end;
  94. constructor TIdTCPClient.Create(AOwner: TComponent);
  95. begin
  96. inherited Create(AOwner);
  97. FBoundPort := BoundPortDefault;
  98. end;
  99. destructor TIdTCPClient.Destroy;
  100. begin
  101. inherited Destroy;
  102. end;
  103. procedure TIdTCPClient.DoOnConnected;
  104. begin
  105. if Assigned(OnConnected) then begin
  106. OnConnected(Self);
  107. end;
  108. end;
  109. procedure TIdTCPClient.SetHost(const Value: string);
  110. begin
  111. FHost := Value;
  112. end;
  113. procedure TIdTCPClient.SetPort(const Value: integer);
  114. begin
  115. FPort := Value;
  116. end;
  117. end.