delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback



Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi








Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


SmartThreadLib example: Using blocking Indy sockets in a threadGo to <B>Erwin</B> Molendijk's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi 6.x (or higher)
Category:
Object Pascal
Skill Level:
Scoring:
Last Update:
02/17/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet smart thread smartthread indy blocking socket tcp client
Times Scored:
5
Visits:
6578
Uploader: Erwin Molendijk
Company: Carvix & Delfer
Reference: N/A
 
Question/Problem/Abstract:
This is an example on how to use the SmartThreadLib. It provides a class called TTCPSmartThread. This thread contains some basic routines to perform TCP communication using blocking sockets.
Answer:



-----------------------------------------------------------------------
Below are the folling files:
TCPSmartThread.pas   -  The unit
main.pas             -  demo showing how to use it
-----------------------------------------------------------------------


{ Smart Thread Lib - TCP example
  Copyright (c) 2002 by DelphiFactory Netherlands BV

  What is it:
  Provides an easy way to use Indy blocking TCP socket client.

  Usage:
  Create your TCP client threads as TTCPSmartThreads and manage them
  using the SmartThreadManager global object.

  Download SmartThreadLib at:
  http://www.delphi3000.com/articles/article_3046.asp

  More about blocking sockets and indy:
  http://www.hower.org/Kudzu/Articles/IntroToIndy/
}

unit TCPSmartThread;

interface

uses
  SysUtils, SmartThreadLib, IdTCPClient, IdException;

resourcestring
  STCPTimedOut = 'Time out while waiting for TCP/IP data';

type
  TTCPSmartThread = class(TSmartThread)
  private
    FWaitDelay    : Integer;  { time slice during waiting (msec) }
    FMaxWaitCount : Integer;
    FTCP : TIdTCPClient;
  protected
    procedure SmartExecute; override;
    procedure TCPExecute; virtual; abstract;

    procedure Connect(const Host : string; const Port : Integer);
    procedure Disconnect;
    procedure WaitFor(const S : string);
    procedure Write(const S : string);
    procedure WaitForAndWrite(const WaitStr, SendStr : string);
    function  ReadLn : string;
  end;

implementation

{ TSmartTCP }

procedure TTCPSmartThread.Connect(const Host: string; const Port: Integer);
begin
  // Disconnect if needed
  Disconnect;

  // setup connection info
  FTCP.Host := Host;
  FTCP.Port := Port;

  // Connect
  FTCP.Connect;

  Check;
end;

procedure TTCPSmartThread.Disconnect;
begin
  Check;
  // disconnect if connected
  if FTCP.Connected then
    FTCP.Disconnect;
  Check;
end;

function TTCPSmartThread.ReadLn: string;
{ Reads a string from the connection.
  The string must be terminated by a LF (#10)
}
const
  EndOfLineMarker = #10;
var
  I : Integer;
begin
  I := 0;
  Repeat
    // raise exception if we need to stop
    Check;
    // try to read data
    Result := FTCP.ReadLn(EndOfLineMarker,FWaitDelay);
    // increase the try counter
    Inc(I);
    // exit loop after to many tries, or if data found
  until (not FTCP.ReadLnTimedOut) or (I>FMaxWaitCount);
  // raise an exception if the read data timed out
  if FTCP.ReadLnTimedOut then
    raise EIdResponseError.Create('time out');
  // perform check
  Check;
end;

procedure TTCPSmartThread.SmartExecute;
begin
  FWaitDelay   := 100;
  FMaxWaitCount := 5000 div FWaitDelay;
  FTCP := TIdTCPClient.Create(nil);
  try
    TCPExecute;
  finally
    FTCP.Free;
  end;
end;

procedure TTCPSmartThread.WaitFor(const S: string);
{ This function returns when the string specified by S
  is read from the TCP connection.
  A timeout exception can be raised.
}
var
  I : Integer;
begin
  I := 0;
  Repeat
    // raise exception if we need to stop
    Check;
    // try to read data
    FTCP.ReadLn(S,FWaitDelay);
    // increase number of tries
    Inc(I);
  until (not FTCP.ReadLnTimedOut) or (I>FMaxWaitCount);
  if FTCP.ReadLnTimedOut then
    raise EIdResponseError.Create(STCPTimedOut);
  Check;
end;

procedure TTCPSmartThread.WaitForAndWrite(const WaitStr, SendStr: string);
{ Wait's for a special string and then sends a reply. }
begin
  WaitFor(WaitStr);
  Write(SendStr);
end;

procedure TTCPSmartThread.Write(const S: string);
{ Send a string over the connection }
begin
  Check;
  FTCP.Write(S);
  Check;
end;

end.


{------------------------------------------------------------}
{ Using the TTCPSmartThread to retreive the time and date:   }

unit main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, SmartThreadLib, TCPSmartThread, IdException;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure OnMessage(Sender : TObject; const AMessage : string);
  public
    { Public declarations }
  end;

type
  TTestThread = class(TTCPSmartThread)
  protected
    procedure TCPExecute; override;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TTestThread }

procedure TTestThread.TCPExecute;
begin
  Connect('132.163.4.101',13);
  while True do Msg(Readln);
  Disconnect;
end;

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  SmartThreadManager.OnMessage := OnMessage;
  TTestThread.Create;
end;

procedure TForm1.OnMessage(Sender: TObject; const AMessage: string);
begin
  Memo1.lines.add(AMessage);
end;

end.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


   


  Community Ad of
M. Shkolnik
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)