🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.internet.socket
6 messages
6 total messages Started by J�nior B. Dymow Fri, 16 Jul 2004 18:51
Indy TIdTCPServer creation at untime
#5585
Author: J�nior B. Dymow
Date: Fri, 16 Jul 2004 18:51
33 lines
1935 bytes
Hi,

    I'm using Indy 9, and I'm having some problems when I create a
TIdTCPServer at runtime. It works fine when I create it at designtime, but
in runtime it doesn't receive any connection. I really need to use it at
runtime, since may app runs as a service and doesn't have a form. This is
the that code I'm using in my teste application (this is not the service,
just an application to discover what's happening).

        ApServerSocket = new TIdTCPServer(Application);
        ApServerSocket->Active= false;
        ApServerSocket->DefaultPort= 5001;
        ApServerSocket->OnConnect= vServerSocketConnectEvent;
        ApServerSocket->Active= true;


void __fastcall TForm1::vServerSocketConnectEvent(TIdPeerThread *kpThread)
{
    ShowMessage("This host has been connected: " +
kpThread->Connection->LocalName);
}

    Am I doing something wrong? At design time, I assign exactly the same
function to the OnConnect event and it works properly.

    Thanks

=======================
J�nior B. Dymow
Software Designer
Specto Pain�is Eletr�nicos
junior@specto.com.br
=======================
Re: Indy TIdTCPServer creation at untime
#5586
Author: Remy Lebeau (TeamB)
Date: Fri, 16 Jul 2004 19:52
44 lines
2523 bytes
"J�nior B. Dymow" <junior@specto.com.br> wrote in message
news:40f822f9@newsgroups.borland.com...

> It works fine when I create it at designtime, but
> in runtime it doesn't receive any connection.

Please elaborate.

> I really need to use it at runtime, since may app
> runs as a service and doesn't have a form.

TService derives from TDataModule, and as such you can place the
TIdTCPServer on the service at design time just like you can with a form.

> void __fastcall TForm1::vServerSocketConnectEvent(TIdPeerThread *kpThread)
> {
>     ShowMessage("This host has been connected: " +
> kpThread->Connection->LocalName);
> }

Do not use ShowMessage() here.  It displays a VCL form, and thus is not
thread-safe.  The OnConnect event is triggered in the context of the worker
thread specified by the AThread parameter, not in the context of the main
VCL thread.  If you need to use ShowMessage(), you have to use the thread's
Synchronize() method so that the VCL form displayed will work properly.
Otherwise, use a thread-safe dialog, such as the Win32 API's MessageBox()
function instead.

Also, just an FYI, the LocalName property will display the server's name,
not the client's.  You have to look up the client's name manually:

    void __fastcall TForm1::vServerSocketConnectEvent(TIdPeerThread
*AThread)
    {
        AnsiString IP = AThread->Connection->Socket->Binding->PeerIP;
        AnsiString Host = GStack->WSGetHostByAddr(IP);
        AnsiString Msg = "This host has been connected: " + IP + ", " +
Host;
        ::MessageBox(NULL, Msg.c_str(), "Connected", MB_OK);
    }


Gambit
Re: Indy TIdTCPServer creation at untime
#5587
Author: J�nior B. Dymow
Date: Fri, 16 Jul 2004 20:36
71 lines
3541 bytes
Thanks by the tips.
But as I said, the code I posted is just a test app, not my real application
(but your tips about threads are very useful). The point is: creating the
TIdTCPServer object dinamically doesn't works. When I connect a client
application, the answer I have is: "No connection could be made because the
target machine refused it (10061), on API 'connect' (I'm translating it from
my native language, but I think you'll understand it :-). When the component
is on the form (with the same port, and the same OnExecute function
assigned), it works fine.
    Do you know what's happening?

Thanks,

=======================
J�nior B. Dymow
Desenvolvimento
Specto Pain�is Eletr�nicos
junior@specto.com.br
Fone / Fax: (48) 334-8064
=======================
"Remy Lebeau (TeamB)" <gambit47.no.spam@no.spam.yahoo.com> escreveu na
mensagem news:40f831be$4@newsgroups.borland.com...
>
> "J�nior B. Dymow" <junior@specto.com.br> wrote in message
> news:40f822f9@newsgroups.borland.com...
>
> > It works fine when I create it at designtime, but
> > in runtime it doesn't receive any connection.
>
> Please elaborate.
>
> > I really need to use it at runtime, since may app
> > runs as a service and doesn't have a form.
>
> TService derives from TDataModule, and as such you can place the
> TIdTCPServer on the service at design time just like you can with a form.
>
> > void __fastcall TForm1::vServerSocketConnectEvent(TIdPeerThread
*kpThread)
> > {
> >     ShowMessage("This host has been connected: " +
> > kpThread->Connection->LocalName);
> > }
>
> Do not use ShowMessage() here.  It displays a VCL form, and thus is not
> thread-safe.  The OnConnect event is triggered in the context of the
worker
> thread specified by the AThread parameter, not in the context of the main
> VCL thread.  If you need to use ShowMessage(), you have to use the
thread's
> Synchronize() method so that the VCL form displayed will work properly.
> Otherwise, use a thread-safe dialog, such as the Win32 API's MessageBox()
> function instead.
>
> Also, just an FYI, the LocalName property will display the server's name,
> not the client's.  You have to look up the client's name manually:
>
>     void __fastcall TForm1::vServerSocketConnectEvent(TIdPeerThread
> *AThread)
>     {
>         AnsiString IP = AThread->Connection->Socket->Binding->PeerIP;
>         AnsiString Host = GStack->WSGetHostByAddr(IP);
>         AnsiString Msg = "This host has been connected: " + IP + ", " +
> Host;
>         ::MessageBox(NULL, Msg.c_str(), "Connected", MB_OK);
>     }
>
>
> Gambit
>
>
Re: Indy TIdTCPServer creation at untime
#5588
Author: Remy Lebeau (TeamB)
Date: Fri, 16 Jul 2004 20:51
23 lines
1634 bytes
"J�nior B. Dymow" <junior@specto.com.br> wrote in message
news:40f83bb1$1@newsgroups.borland.com...

> The point is: creating the TIdTCPServer object
> dinamically doesn't works.

Yes, it does, when used properly.

> When I connect a client application, the answer I have is:
> "No connection could be made because the target machine
> refused it (10061), on API 'connect'

Did you set up any Bindings first?

> When the component is on the form (with the same port,
> and the same OnExecute function assigned), it works fine.

There is no difference between creating the component at design-time vs.
run-time, other than the DFM streaming.


Gambit
Re: Indy TIdTCPServer creation at untime
#5589
Author: J�nior B. Dymow
Date: Fri, 16 Jul 2004 21:13
62 lines
2819 bytes
> > The point is: creating the TIdTCPServer object
> > dinamically doesn't works.
>
> Yes, it does, when used properly.

            That's what I'm looking for: the correct way to use it...
>
> > When I connect a client application, the answer I have is:
> > "No connection could be made because the target machine
> > refused it (10061), on API 'connect'
>
> Did you set up any Bindings first?

            No, I didn't. Can you please tell me how to do it? But I didn't
do it with the design time component, and it worked. Do you think this can
be the problem?

> > When the component is on the form (with the same port,
> > and the same OnExecute function assigned), it works fine.
>
> There is no difference between creating the component at design-time vs.
> run-time, other than the DFM streaming.
>
>
> Gambit
>
>
=======================
J�nior B. Dymow
Desenvolvimento
Specto Pain�is Eletr�nicos
junior@specto.com.br
Fone / Fax: (48) 334-8064
=======================

"Remy Lebeau (TeamB)" <gambit47.no.spam@no.spam.yahoo.com> escreveu na
mensagem news:40f83fb3$1@newsgroups.borland.com...
>
> "J�nior B. Dymow" <junior@specto.com.br> wrote in message
> news:40f83bb1$1@newsgroups.borland.com...
>
> > The point is: creating the TIdTCPServer object
> > dinamically doesn't works.
>
> Yes, it does, when used properly.
>
> > When I connect a client application, the answer I have is:
> > "No connection could be made because the target machine
> > refused it (10061), on API 'connect'
>
> Did you set up any Bindings first?
>
> > When the component is on the form (with the same port,
> > and the same OnExecute function assigned), it works fine.
>
> There is no difference between creating the component at design-time vs.
> run-time, other than the DFM streaming.
>
>
> Gambit
>
>
Re: Indy TIdTCPServer creation at untime
#5590
Author: Remy Lebeau (TeamB)
Date: Fri, 16 Jul 2004 21:38
19 lines
1526 bytes
"J�nior B. Dymow" <junior@specto.com.br> wrote in message
news:40f84444@newsgroups.borland.com...

>             No, I didn't. Can you please tell me how to do it?

Look at the Bindings property.  It controls which local IPs and ports are
actually listened on.

> But I didn't do it with the design time component, and
> it worked. Do you think this can be the problem?

Probably not, actually.

Can you please show your ACTUAL code, including showing where you are
calling the code from?


Gambit
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads