Re: I'm going mad with a simple TCP connection...

Giganews Newsgroups
Subject: Re: I'm going mad with a simple TCP connection...
Posted by:  Remy Lebeau (TeamB) (gambit…@yahoo.com)
Date: Wed, 9 Jul 2003

"Michele" <giovanni.costantino…@tin.it> wrote in message
news:ABD57F957A76E240giovanni.costantino…@tin.it...

> memo.Lines.Add('OnExecute');

The OnExecute event is triggered in the context of the TIdPeerThread that it
belongs to.  It is not triggered in the context of the main VCL thread.  As
such, you should not be accessing objects from the main VCL thread directly.
You need to use the thread's Synchronize() method instead.

> AThread.Connection.ReadBuffer(str,sizeof(str));          //

You should not be reading a string like that.  You should be using the
ReadString() or ReadLn() method instead.

> !!!!!!!!!!!!!this hang the process!!!!!!!!!!!!
> memo.Lines.Add(str);

Again, you cannot access the Memo from the OnExecute handler directly.  You
need to use Synchronize() instead, ie:

    var str: string;

    procedure TForm1.TCPServerExecute(AThread: TIdPeerThread);
    begin
        str := AThread.Connection.ReadLn;    // or however you decide to
read the string...
        AThread.Synchronize(UpdateTheMemo);
    end;

    procedure TForm1.UpdateTheMemo;
    begin
        memo.Lines.Add(str);
    end;

> TCPClient.WriteLn('Hello world!');

Since you are using WriteLn(), you should be using ReadLn() on the server to
read the string.

Gambit

Replies

In response to

I'm going mad with a simple TCP connection... posted by Michele on Wed, 9 Jul 2003