Newsgroups : Borland : borland.public.delphi.internet.winsock : 2008 Jan : Re: Looking for very basic indy 10 server examples

www.cryer.info
Managed Newsgroup Archive

Re: Looking for very basic indy 10 server examples

Subject:Re: Looking for very basic indy 10 server examples
Posted by:"Remy Lebeau (TeamB)" (no.spam@no.spam.com)
Date:Thu, 24 Jan 2008 11:00:47

"Mark Smith" <marksmith@jungle-monkey.com> wrote in message
news:4797f6e8@newsgroups.borland.com...

> I'm trying to learn how to use indy. A lot of the examples I
> can find online assume I am embedding the indy component
> within a form.

They work exactly the same whether you drop them on a TForm or TDataModule
at design-time, or instantiate them dynamically in your run-time code
instead.

> I just want a simple console application to be able to act as a server.

Given the code you have shown, your console app is going to terminate right
away.  You are creating the server object fine, but then you let the DPR
code exit without waiting on any kind of event or other termination
triggers.  You need to perform some kind of waiting in order for the console
process to stay running for any length of time.

> I can't even get as far as creating the server:

Yes, you are.  You are just not doing anything with it.

    program ServerTest;

    {$APPTYPE CONSOLE}

    uses
        SysUtils, IdTCPServer;

    type
        // VCL event handlers need to be methods of a class
        TMyServerEvents = class
        public
            procedure ServerExecute(AThread: TIdPeerThread);
        end;

    procedure TMyServerEvents.ServerExecute(AThread: TIdPeerThread);
    var
        S: String;
    begin
        S := AThread.Connection.ReadLn;
        WriteLn(Format('%s: %s', [AThread.Connection.Socket.Binding.PeerIP,
S]));
    end;

    var
        svr: TIdTCPServer;

        events: TMyServerEvents;

    begin
        svr := TIdTCPServer.Create(nil);
        try
            events := TMyServerEvents.Create;
            try
                svr.Port := 12345;
                svr.OnExecute := events.ServerExecute;
                svr.Active := True;
                try
                    // wait for some signal that the app should shut down...
                finally
                    svr.Active := False;
                end;
            finally
                events.Free;
            end;
        finally
            svr.Free;
        end;
    end.

To help you with using the server and its events, you might want to consider
putting the server into a TDataModule at design-time, and then you can
create the TDataModule at run-time.


Gambit

Replies:

In response to:

www.cryer.info
Managed Newsgroup Archive