Indy TCP Server exceptions

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Indy TCP Server exceptions

Postby gbrandt » Wed Mar 03, 2010 1:51 pm

FIXED: see second entry


Hi,

I am asking alot of question lately :-) Soon I hope to help answer some as well.

Windows 7, Builder 2010, standard install

I have the following code:

Code: Select all
      try
      {
         mTCPServer = new TIdTCPServer( NULL );
         mTCPServer->OnExecute =  MyExecute;
         mTCPServer->DefaultPort = 32790;
         mTCPServer->Active = true;
      }
      catch( ... )
      {
      }

in my constructor.

When I run this code I get two exceptions when I turn the server active:

Code: Select all
First chance exception at $75FE9617. Exception class EIdSocketError with message
'Socket Error # 10048
Address already in use.'.
Process PhaseManager.exe (3104)

and then

Code: Select all
First chance exception at $75FE9617. Exception class EIdCouldNotBindSocket with message 'Could not bind socket. Address and port are already in use.'. Process PhaseManager.exe (3104)

But the code works perfectly. I can telnet into 32790 and get a response (my OnExecute simply echos what I type with a timestamp).

I really should not be getting these exceptions because the open and bind appear to be working perfectly. Any ideas?

I do not get these exceptions if I drop a TcpServer on my unit, only if I 'new' it.

Thanks,
Gregor
gbrandt
Top Poster
Top Poster
 
Posts: 41
Joined: Thu Feb 11, 2010 12:45 pm

Re: Indy TCP Server exceptions

Postby gbrandt » Wed Mar 03, 2010 2:20 pm

[quote="gbrandt"]

Code: Select all
      try
      {
         mTCPServer = new TIdTCPServer( NULL );
         mTCPServer->OnExecute =  MyExecute;
         mTCPServer->DefaultPort = 32790;
         mTCPServer->Active = true;
      }
      catch( ... )
      {
      }


fixed by changing above code into:

Code: Select all
      try
      {
         mTCPServer = new TIdTCPServer( this );
         mTCPServer->OnExecute =  MyExecute;
         mTCPServer->DefaultPort = 32790;
         mTCPServer->Active = true;
      }
      catch( ... )
      {
      }


If I used owner instead of this, I would get my component as well as the TIDTCPServer component on my unit...wierd.

Gregor
gbrandt
Top Poster
Top Poster
 
Posts: 41
Joined: Thu Feb 11, 2010 12:45 pm

Re: Indy TCP Server exceptions

Postby gambit47 » Wed Mar 03, 2010 6:07 pm

gbrandt wrote:fixed by changing above code into:


Changing the assigned Owner of the TIdTCPServer component has no effect whatsoever on how it manages its sockets. Something else is happening.
Remy Lebeau (TeamB)
http://www.lebeausoftware.org
User avatar
gambit47
BCBJ Author
BCBJ Author
 
Posts: 472
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: Indy TCP Server exceptions

Postby gambit47 » Wed Mar 03, 2010 6:11 pm

gbrandt wrote:When I run this code I get two exceptions when I turn the server active


That error means that another process is already using port 32790 at the time when you activate your server.

gbrandt wrote:But the code works perfectly.


That is not possible. If a bind failure occurs when activating the server, the listening sockets are shut down, the server is deactivated, and the exception is raised into your code.

gbrandt wrote:I can telnet into 32790 and get a response (my OnExecute simply echos what I type with a timestamp).


Check if you have multiple instances of your app running at the same time.

gbrandt wrote:I do not get these exceptions if I drop a TcpServer on my unit, only if I 'new' it.


How the TIdTCPServer object is created makes no difference whatsoever on how its sockets are managed.
Remy Lebeau (TeamB)
http://www.lebeausoftware.org
User avatar
gambit47
BCBJ Author
BCBJ Author
 
Posts: 472
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: Indy TCP Server exceptions

Postby gbrandt » Thu Mar 04, 2010 11:12 am

gambit47 wrote:That error means that another process is already using port 32790 at the time when you activate your server.


Nothing else was using the port, I even chose other random port numbers to verify.

gambit47 wrote:
gbrandt wrote:But the code works perfectly.


That is not possible. If a bind failure occurs when activating the server, the listening sockets are shut down, the server is deactivated, and the exception is raised into your code.


I know its not possible, but it did happen. Could my constructor have been called twice? I placed the code in the constructor of the component. Does VCL do something weird here? And again, it ONLY happened when the argument was NULL, not when the argument was the parent Component.

gambit wrote:Check if you have multiple instances of your app running at the same time.


Nope, only one instance.

Follow-up: thinking that my constructor could be called twice I checked and found ValidCtrCheck allocating an instance of my class and not deleting it. The pointer is not used anywhere, its not passed out of the function. So I added a delete to the function but that did not correct anything.

Gregor
gbrandt
Top Poster
Top Poster
 
Posts: 41
Joined: Thu Feb 11, 2010 12:45 pm

Re: Indy TCP Server exceptions

Postby gambit47 » Thu Mar 04, 2010 6:04 pm

gbrandt wrote:Nothing else was using the port, I even chose other random port numbers to verify.


Well, the error message (which came from the OS, BTW) stated otherwise. It said the port was in use.

gbrandt wrote:I know its not possible, but it did happen. Could my constructor have been called twice?


That would only happen if you left the design-time component in your app, as well as the dynamically-created one in your code. But even if that happened, that would not typically cause the error since you are only activating 1 server object in your code. Unless you are activating the dynamic object in your code, and your design-time object was set to the same port and had Active=True in the Object Inspector. That would be the only way I could see both objects being active at the same time.

gbrandt wrote:Follow-up: thinking that my constructor could be called twice I checked and found ValidCtrCheck allocating an instance of my class and not deleting it.


ValidCtrCheck() is not actually called by anything. It serves only as a compile-time checker to make sure your component class does not contain any abstract methods that have not been overwritten yet. A TObject-based class cannot be allocated on the stack, so ValidCtrCheck() has to use the 'new' operator, but since it is not actually being called, nothing is being allocated at run-time.
Remy Lebeau (TeamB)
http://www.lebeausoftware.org
User avatar
gambit47
BCBJ Author
BCBJ Author
 
Posts: 472
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: Indy TCP Server exceptions

Postby gbrandt » Fri Mar 05, 2010 4:26 pm

It turns out I was allocating and making active the server in my constructor. I wrapped that with a check to see if I was in designing mode and all is well.
gbrandt
Top Poster
Top Poster
 
Posts: 41
Joined: Thu Feb 11, 2010 12:45 pm


Return to Technical

Who is online

Users browsing this forum: No registered users and 2 guests

cron