Newsgroups : Borland : borland.public.delphi.internet.winsock : 2007 Mar : Re: How to set the listen port for the TIdTCPServer?

www.cryer.info
Managed Newsgroup Archive

Re: How to set the listen port for the TIdTCPServer?

Subject:Re: How to set the listen port for the TIdTCPServer?
Posted by:"Remy Lebeau (TeamB)" (no.spam@no.spam.com)
Date:Mon, 26 Mar 2007 16:21:32

"Bo Berglund" <bo.berglund@telia.com> wrote in message
news:aifg03hng830a706ci4112krnoc1c5kns7@4ax.com...

> I am still thinking of having an eternal loop in the OnConnect in
> order to grab the handle to the client so I can send commands
> to it during the time it stays connected.

You don't need to do that.

> I don't want to do that in the main line of the code so just setting
a
> local variable to the AThread object and later use that to send data
> to the client

Do not do that, either.

> seems to me to use the thread of the main app to execute the
> Connection.Write method and thus suffer from the blocking
> behaviour.

Write() runs in the context of whatever thread is calling it.

> Instead I plan to put outgoing data into a list that is read in the
> internal loop in OnConnect and is written by the mainline
> code.

Do not loop in the OnConnect event.  Assuming your data is
string-based, simply put a TIdThreadSafeStringList into the
TIdPeerThread's Data property, then write to it whenever needed in any
thread, and finally have the OnExecute event send whatever is
currently in the list.  For example:

    procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
    begin
        AThread.Data := TIdThreadSafeStringList.Create;
    end;

    procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
    var
        List: TIdStringList;
    begin
        List := TIdThreadSafeStringList(AThread.Data).Lock;
        try
            AThread.Connection.WriteStrings(List);
        finally
            List.Clear;
            TIdThreadSafeStringList(AThread.Data).Unlock;
        end;
    end;

    procedure TForm1.Button1Click(Sender: TObject);
    var
        List: TList;
        Thread: TIdPeerThread;
    begin
        List := IdTCPServer1.Threads.LockList;
        try
            Thread := TIdPeerThread(List[SomeIndex]);
            TIdThreadSafeStringList(Thread.Data).Add('your data
here');
        finally
            IdTCPServer1.Threads.UnlockList;
        end;
    end;

> When looking at the server example for Indy9 I noticed that they
> have dropped a TIdThreadMgrDefault component on the form
> and connected it to the TIdTCPServer. What is the purpose of this?

The ThreadMgr is what manages the threads that the server uses
internally for client connections.  If you do not assign a ThreadMgr,
one is created automatically when the server is activated.

> do I need one such object per server

It is possible to share a single ThreadMgr amongst multiple servers.
I have never done it before, though.


Gambit

Replies:

none

In response to:

www.cryer.info
Managed Newsgroup Archive