🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #2920

Re: Unresolved external 'Idantifreeze::TIdAntiFreeze:

#2920
From: Remy Lebeau (TeamB)
Date: Wed, 11 Jan 2006 10:17
359 lines
11811 bytes
"Liu Shan" <liushan@ps-electronic.com> wrote in message
news:43c4a871@newsgroups.borland.com...

> Once I click on "Start" button, I got "Error starting server", I
> click "Strat" button again, got "Access violation at address 40006834
> in module 'rtl60.bpl". read of address....." or after "Error starting
server",
> I close application, also got acccess violation.

Did you try debugging the code yet to find the EXACT lines where the errors
are occuring?

> void __fastcall TfrmMain::FormCreate(TObject *Sender)

DO NOT use the OnCreate event in C++!  It is a Delphi idiom that produces
illegal behavior in C++, as it can be triggered before the constructor is
called.  Use the actual constructor instead.  The same goes for the
OnDestroy event as well - it produces illegal behavior because it can be
triggered after the destructor is called.

Try the following code:


    __fastcall TfrmMain::TfrmMain(TComponent *Owner)
        : TForm(Owner)
    {
        fErrors =  new TStringList;
        PopulateIPAddresses();
    }

    __fastcall TfrmMain::~TfrmMain()
    {
        if( fErrors )
            delete fErrors;
    }

    void TfrmMain::PopulateIPAddresses()
    {
        lbIPs->Items->Assign(GStack->LocalAddresses);
        lbIPs->Items->Add("127.0.0.1");

        cboPorts->Items->BeginUpdate();
        try
        {
            cboPorts->Items->Add("RSBindingAny");

            TList *PortList = IdPorts();
            for(int i = 0; i < PortList->Count; ++i)
            {
                int port = reinterpret_cast<int>(PortList->Items[i]);
                cboPorts->Items->Add(PortDescription(port));
            }
        }
        __finally
        {
            cboPorts->Items->EndUpdate();
        }
    }

    AnsiString TfrmMain::PortDescription(int PortNumber)
    {
        AnsiString Result;

        // there is a bug in Indy 9's WSGetServByPort() that can return
        // an invalid TStrings* pointer if a failure occurs internally...

        TStrings *tStr = GStack->WSGetServByPort(PortNumber);
        if( tStr )
        {
            try
            {
                try
                {
                    if( tStr->Count > 0 )
                        Result = AnsiString(PortNumber) + ": " +
tStr->CommaText;
                    else
                        Result = PortNumber;
                }
                __finally
                {
                    delete tStr;
                }
            }
            catch(const Exception &)
            {
                Result = PortNumber;
            }
        }

        return Result;
    }

    bool TfrmMain::StopServer()
    {
        IdTCPServer->Active = false;
        fServerRunning = IdTCPServer->Active;

        if( fServerRunning )
        {
            StatusBar->SimpleText = "Server running";
            lbProcesses->Items->Append("Server not stopped");
        }
        else
        {
            StatusBar->SimpleText = "Server stopped";
            lbProcesses->Items->Append("Server stopped");
        }

        return !fServerRunning;
    }

    bool TfrmMain::StartServer()
    {
        if( !StopServer() )
        {
            fErrors->Append("Error stopping server");
            return false;
        }

        IdTCPServer->Bindings->Clear();
        IdTCPServer->DefaultPort = StrToInt(edtPort->Text);

        TStringList *SL = new TStringList;
        try
        {
            try
            {
                for(int i = 0; i< lbIPs->Count; ++i)
                {
                    if( lbIPs->Checked[i] )
                    {
                        TIdSocketHandle *Binding =
IdTCPServer->Bindings->Add();
                        Binding->IP = lbIPs->Items->Strings[i];
                        SL->Append("Server bound to IP " + Binding->IP + "
on port " + edtPort->Text);
                    }
                }

                if( IdTCPServer->Bindings->Count > 0 )
                {
                    IdTCPServer->Active = true;
                    fServerRunning = IdTCPServer->Active;
                }

                if( fServerRunning )
                {
                    lbProcesses->Items->AddStrings(SL);
                    lbProcesses->Items->Append("Server started");
                    StatusBar->SimpleText = "Server running";
                }
                else
                {
                    lbProcesses->Items->Append("Server not started");
                    StatusBar->SimpleText = "Server stopped";
                }
            }
            __finally
            {
                delete SL;
            }
        }
        catch(const Exception &e)
        {
            IdTCPServer1->Active = false;
            fServerRunning = IdTCPServer1->Active;
            lbProcesses->Items->Append("Server not started");
            fErrors->Append(e.Message);
        }

        return fServerRunning;
    }

    void __fastcall TfrmMain::btnStartServerClick(TObject *Sender)
    {
        for(int i = 0; i < lbIPs->Count; ++i)
        {
            if( lbIPs->Checked[i] )
            {
                fErrors->Clear();

                if( !StartServer() )
                    ShowMessage("Error starting server\t\t" +
fErrors->CommaText);
                else
                    ShowMessage("Server started successfully!");

                return;
            }
        }

        ShowMessage("Cannot proceed until you select at least one IP to
bind!");
    }

    void __fastcall TfrmMain::cboPortsChange(TObject *Sender)
    {
        int index = cboPorts->ItemIndex;
        if( index > -1 )
        {
            AnsiString strPort = cboPorts->Items->Strings[index];
            edtPort->Text = Fetch(strPort, ":");
        }
    }

    void __fastcall TfrmMain::btnStopServerClick(TObject *Sender)
    {
        fErrors->Clear();

        if( fServerRunning )
        {
            if( !StopServer() )
                ShowMessage("Error stopping server\t\t" +
fErrors->CommaText);
            else
                ShowMessage("Server stopped successfully");
        }
        else
            ShowMessage("Server it not running - no need to stop !");
    }

    void __fastcall TfrmMain::btnClearMessagesClick(TObject *Sender)
    {
        lbProcesses->Clear();
    }

    void __fastcall TfrmMain::IdTCPServerExecute(TIdPeerThread *AThread)
    {
        AnsiString Command = AThread->Connection->ReadLn();

        if( AnsiSameText(Command, "TIME") )
        {
            AThread->Connection->WriteLn(Now().FormatString("hh:nn:ss"));
        }
        else if( AnsiSameText(Command, "DATE") )
        {

AThread->Connection->WriteLn(Date().FormatString("dd/mmm/yyyy"));
        }
        else if( AnsiSameText(Command, "TICKCOUNT") )
        {
            AThread->Connection->WriteInteger(GetTickCount());
        }
        else if( AnsiSameText(Command, "QUIT") )
        {
            AThread->Connection->WriteLn("Goodbye!");
            AThread->Connection->DisconnectSocket();
        }
        else
            AThread->Connection->WriteLn("Command not recognised - try
again!");
    }

    void __fastcall TfrmMain::IdTCPServerConnect(TIdPeerThread *AThread)
    {
        AThread->Connection->WriteLn("Welcome to the Indy TCP basic
client/server demo");
    }

    void __fastcall TfrmMain::btnExitClick(TObject *Sender)
    {
        Close();
    }


With that said, if you use the TIdTCPServer's Greeting and CommandHandlers
properties, you can get rid of the TIdTCPServer's OnExecute event handler
altogether.  These properties can be configured at design-time to do much of
the run-time work automatically for you:


    --- TfrmMain.cpp ---

    void __fastcall TfrmMain::IdTCPServer1TimeCommand(TIdCommand *ASender)
    {
        ASender->Reply->SetReply(200, Now().FormatString("hh:nn:ss"));
    }

    void __fastcall TfrmMain::IdTCPServer1DateCommand(TIdCommand *ASender)
    {
        ASender->Reply->SetReply(200, Date().FormatString("dd/mmm/yyyy"));
    }

    void __fastcall TfrmMain::IdTCPServer1TickCountCommand(TIdCommand
*ASender)
    {
        ASender->Reply->SetReply(200, GetTickCount());
    }


    --- TfrmMain.dfm ---

    ...
    object IdTCPServer1: TIdTCPServer
        ...
        CommandHandlers = <
            item
                CmdDelimiter = ' '
                Command = 'TIME'
                Disconnect = False
                Name = 'Time'
                OnCommand = IdTCPServer1TimeCommand
                ParamDelimiter = ' '
                ReplyExceptionCode = 0
                ReplyNormal.NumericCode = 200
                Tag = 0
            end
            item
                CmdDelimiter = ' '
                Command = 'DATE'
                Disconnect = False
                Name = 'Date'
                OnCommand = IdTCPServer1DateCommand
                ParamDelimiter = ' '
                ReplyExceptionCode = 0
                ReplyNormal.NumericCode = 200
                Tag = 0
            end
            item
                CmdDelimiter = ' '
                Command = 'TICKCOUNT'
                Disconnect = False
                Name = 'TickCount'
                OnCommand = IdTCPServer1TickCountCommand
                ParamDelimiter = ' '
                ReplyExceptionCode = 0
                ReplyNormal.NumericCode = 200
                Tag = 0
            end
            item
                CmdDelimiter = ' '
                Command = 'QUIT'
                Disconnect = True
                Name = 'Quit'
                ParamDelimiter = ' '
                ReplyExceptionCode = 0
                ReplyNormal.NumericCode = 200
                ReplyNormal.Text.Strings = (
                    'Goodbye!')
                Tag = 0
            end>
        ...
        Greeting.NumericCode = 200
        Greeting.Text.Strings = (
            'Welcome to the Indy TCP basic client/server demo')
        ...
        ReplyExceptionCode = 500
        ...
        ReplyUnknownCommand.NumericCode = 501
        ReplyUnknownCommand.Text.Strings = (
            'Command not recognised - try again!')
        ...
    end
    ...


Gambit
Technical Details
Message-ID: <43c4da9b@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!news.moat.net!pln-e!lotsanews.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <43bcec76$1@newsgroups.borland.com> <43bd5f66$1@newsgroups.borland.com> <43bdcbd3$1@newsgroups.borland.com> <43bdd68e@newsgroups.borland.com> <43be37e8@newsgroups.borland.com> <43bebfd0$1@newsgroups.borland.com> <43c1be5a$1@newsgroups.borland.com> <43c23f02@newsgroups.borland.com> <43c47703$1@newsgroups.borland.com> <43c496ed$1@newsgroups.borland.com> <43c4a871@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,a9ad1d742dfc75bf
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <43bcec76$1@newsgroups.borland.com> <43bd5f66$1@newsgroups.borland.com> <43bdcbd3$1@newsgroups.borland.com> <43bdd68e@newsgroups.borland.com> <43be37e8@newsgroups.borland.com> <43bebfd0$1@newsgroups.borland.com> <43c1be5a$1@newsgroups.borland.com> <43c23f02@newsgroups.borland.com> <43c47703$1@newsgroups.borland.com> <43c496ed$1@newsgroups.borland.com> <43c4a871@newsgroups.borland.com>
Subject: Re: Unresolved external 'Idantifreeze::TIdAntiFreeze:
Date: Wed, 11 Jan 2006 02:17:15 -0800
Lines: 361
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <43c4da9b@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1136974491 68.69.17.41 (11 Jan 2006 02:14:51 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!news.moat.net!pln-e!lotsanews.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail