Use of TTimer with Indy TIDTcpClient

delphi

    Sponsored Links

    Next

  • 1. Is 3 Ports much Load ?
    My application uses 3 ports. (1) FTP Server and FTP Client for transfering files. (2) Serversocket + ClientSocket for communication to Application on server (3) Serversocket + ClientSocket for communication to other nodes Though the Clients in all 3 cases are inactive for 80% of the time, the Server's are active throughout. I am new to socket programming and would like to know if using three ports would put any strain on the computers resources. Regards Allan
  • 2. System.Net.Sockets.TcpClient <---> Indy
    Hello! I am working in a .net project which uses clean System.Net.Sockets.TcpClient sockets. It needs to communicate with a simply server which uses win32 indy 10. The server expects a string. It uses: s:= idTCPServer.Connection.IOHandler.Readln(); and sends a response idTCPServer.Connection.IOHandler.WriteLn(somestring); So, how can I make System.Net.Sockets.TcpClient to clearly communicate with the server? What I am unsure is the fact that .net uses widestrings and indy10 (win32) uses ANSI. So i am searching for 2 .net procedures; procedure SendString(const s:string); begin // using System.Net.Sockets.TcpClient //??? end; and function GetString(): string; begin //System.Net.Sockets.TcpClient //??? end; Some help?
  • 3. Checkuser problem with IdSMTPServer
    Hi, I'm want to use the CheckUser to authentificate the sender of the message (event) of the IdSMTPServer component. My problem is the event is never fired ! I used an Indy : 9.0.14. Is there any problem with this version or anything I miss (Property, etc ...) ? Thanks at all at advance for your help. Alain -- ++sign A.Falanga "The French Frog TeamZed Member"
  • 4. TIdServer and CPU Utilization
    Using two D25K programs, one with an IdTCPServer, and one with an IdTCPClient (and no other components on either), I find that upon connection, the server program goes to near-100% CPU utilization. This is true even when the client is doing nothing (as in, sitting on a debug breakpoint). Disconnect, and the utilization drops to a nominal level (near zero). Is that normal behavior for an IdTCPServer?

Use of TTimer with Indy TIDTcpClient

Postby Marais » Wed, 30 May 2007 04:04:15 GMT

Hi, all-

I have an app which needs to poll a web site every minute or so. I'm using a 
TTimer to pace the poll (the exact time is not at all critical).

In the event handler for the timer, I PostMessage to the main form:

procedure TForm1.Timer1Timer(Sender: TObject);
begin
PostMessage(Form1.Handle, WM_Timer1, 0, 0)
end;

A message handler in the main form then executes the Client.Connect method:

procedure TForm1.HandleTimer1(var Message: TMessage);
begin
GetDataFromServer;
end;

procedure TForm1.GetDataFromServer;
begin
try
Client1.connect(5000);
except
  on e:exception do PostMessage(Form1.Handle, WM_ConnectFailed, 0, 0)
  end;
end;

I use a similar method to respond to the OnConnect event to do the 
Client.Writeln and Client.Readln to request and receive the data.

I go through all this to ensure that the client methods are called only from 
the main thread and not from some other thread, which may be active when the 
events take place. Is this a reasonable way to approach this?

The reason I ask is that my main form is exhibiting a disturbing symptom: a 
TMemo in the main form is not repainting itself 100% when I slide another 
window over my form. Some artifacts remain, notably in the lower right 
corner between the vertical and horizontal scroll bars.

Any thoughts?

Peter



Re: Use of TTimer with Indy TIDTcpClient

Postby Remy Lebeau (TeamB) » Thu, 31 May 2007 02:43:48 GMT







Any particular reason why you are using TIdTCPClient instead of
TIdHTTP for that?

critical).

You should use a separate worker thread instead for things like that.

only

In this case, your polling would be better suited in its own thread.
You can post notifications to the main thread when you need to notify
it of the polling status and results when needed, but the polling
itself does not need to be in the main thread, and will only slow it
down.

symptom:

As well it should be.  You are performing blocking operations in the
main thread, so it can't process other messages, such as painting
messages, while the socket code is busy.


Gambit



Re: Use of TTimer with Indy TIDTcpClient

Postby Marais » Thu, 31 May 2007 06:59:20 GMT









I oversimplified perhaps a little in the interests of making my posting 
short. In fact, I'm polling a little data acquisition box which needs to 
receive only a string saying "GET / HTTP/V1.0", and then returns a short 
string.


Sounds good - any suggestions where I could find out more about this 
concept? Would I use a Delphi TThread object? In any case, would I use a 
TTimer for the pacing, and if so, how would I direct the timer events to the 
worker thread?




I'm calling TIdTCPClient readln and writeln methods, and I understand how 
these are blocking, but most of the time the main thread is idle waiting for 
the TTimer to fire. It's not blocked in this situation is it?

Thanks,

Marais



Re: Use of TTimer with Indy TIDTcpClient

Postby Marais » Thu, 31 May 2007 20:28:00 GMT

OK-

After sleeping on it, I think I've answered a few of my own questions. My 
problem is becoming more of a threads issue than an Indy one, but because 
writing threaded code is important for Indy users, it seems reasonable to 
carry on with the discussion here.

I know how to create a worker thread in general. In this case, it seems that 
my thread needs a TIdTCPClient and probably a TTimer component as well. I 
think that my remaining question is this: do I place these components on the 
main form in the IDE, or do I instantiate them at run time in the worker 
thread? Presumably there's a canonical approach, but I don't know what it 
is.

Thanks

Marais



Re: Use of TTimer with Indy TIDTcpClient

Postby Remy Lebeau (TeamB) » Fri, 01 Jun 2007 10:50:48 GMT







Yes.


No.  A simple Sleep() within a loop inside the TThread's Execute()
method will work fine.


Gambit



Similar Threads:

1.Ambiguity between 'TTimer' and 'Qextctrls::TTimer'

[C++ Error] XXX.h(13): E2015 Ambiguity between 'TTimer' and
'Qextctrls::TTimer'

how can I solve it?


I need 'TTimer'


2.Using a TTimer without a form

How do I associate code to the onTimer event of a TTimer that's not on a
form?  In my unit I have:

//------------------
interface

procedure tmrDelay_onTimer(Sender: TObject);

var
    tmrDelay : TTimer;

//------------------
implementation

procedure tmrDelay_onTimer(Sender: TObject);
begin
    // do stuff here
end;

//------------------
initialization
    tmrDelay:=TTimer.Create(Application);
    tmrDelay.Enabled:=false;
    tmrDelay.OnTimer:=tmrDelay_onTimer;


and the OnTimer assignment is failing with an Incompatible types message.  I
tried this:

tmrDelay.OnTimer:=TNotifyEvent(tmrDelay_onTimer) and the error changed to
Invalid TypeCast.

I have seen code that does this dynamic assignment when the Timer (and event
procedure) is part of a class (like a form or button), but how can I use it
when it's global to the unit?  Do I have to create a class for it?

ta,
cj


3.getting a component to call a function without using a TTimer

If I want my component to perform a task, say, checking if there is data in 
the inbuffer, on a regular basis, how would I do this without resorting to a 
TTimer. In other words, is there a more elegant way of doing things.

Any ideas will be much appreciated.


4.Indy 10 - TIdTCPClient.BoundIP

Hi,

I use Indy 10.0.52 in Delphi 2005 and try to migrate code which was 
written with Indy 9.

Problem:
For a IdTCPClient I can not use the property BoundIP anymore.
	old: aIdTCPClient.BoundIP

Is there also in Indy 10 the possibility to define the client used IP 
address?

Thanks,
Uli

5.Indy 9 TidTcpClient / TidTcpServer stream problems

I'm trying to have my Client send a data stream to the server,  but the 
server keeps giving Stream Read Error exception.

I can't see what I'm doing wrong,  and any help would be appreciated.
Source is below,  and the exception source line is indicated in the server 
side
code.

Best Regards



Client Side
===========================

//------------------------------------------------------------------------------
// Only call this if item was actually modified,
// Otherwise,  simply call CS_RQ_UnlockTableItem
function TMainForm.CS_NOT_UpdateTableItem(Table : TStorage_Table;
                                          Item  : TStorageItem) : Boolean;
var
Stream : TMemoryStream;
begin
  Screen.Cursor := crHourGlass;
  try
    result := (Client.SendCmd('NOT_UpdateItem ' +
                              Table.ClassName + ' ' +
                              Item.HashStr) = 997);
    if result then
    try
      Stream := TMemoryStream.Create;
      if Item.Write(Stream) then
      begin
        Stream.Position := 0;
        Client.WriteStream(Stream);
      end;
    finally
      Stream.Free;
    end;
  finally
    Screen.Cursor := crDefault;
  end;
end;



Server Side

//------------------------------------------------------------------------------
// Command : NOT_UpdateItem
procedure Tcs_Struct.NOT_UpdateItem(ASender: TIdCommand);
var
Table      : TStorage_Table;
Item       : TStorageItem;
HashStr    : String;
i          : Longint;
aHash      : Cardinal;
s          : String;
Connection : Tcs_Connection;
Stream     : TMemoryStream;
begin
  Connection := ThreadConnection[ASender.Thread];
  if Connection = nil then
  begin
    ASender.Reply.SetReply(-997, 'Unrecognized Connection');
    ASender.SendReply;
    exit;
  end;

  Table := nil;
  s := ASender.Params[0];

  // Find the Table
  if s = 'TStock' then
    Table := fStock;

  if Table = nil then
  begin
    ASender.Reply.SetReply(9999, 'Invalid Table Identifier');
    ASender.SendReply;
    exit;
  end;

  HashStr := ASender.Params[1];
  aHash   := StrToInt64Def(HashStr, -1);
  Item    := TStorageItem(Table.HashTable.HashObject[aHash]);
  if Item = nil then
  begin
    ASender.Reply.SetReply(9999, 'Invalid Item Identifier');
    ASender.SendReply;
    exit;
  end;

  // Read the updated data sent by the client
  Stream := TMemoryStream.Create;
  try
    ASender.Reply.SetReply(997, '');
    ASender.SendReply;


   // ####### EXCEPTION OCCURS BELOW #######
    ASender.Thread.Connection.ReadStream(Stream);


Stream.Position := 0;
    Item.Read(Stream);
    fMatStock.storage_WriteListItem(Item);
  finally
    Stream.Free;
    Item.UnLock(Connection.fUserHash);
  end;
end;



6. Indy TidTCPClient with .NET

7. TidTCPClient in Indy 10

8. Timeout on TIdTCPClient never expires (Indy)



Return to delphi

 

Who is online

Users browsing this forum: No registered users and 93 guest