Delphi Indy TCPServer bindings at runtime

Greetings

I cant seem to be able to set the TcpServer's bindings property at runtime. When setting some values in the object inspector at design time , all is well and I am able to connect the the server with no hassle.

The following code was suggested on Delphi.about.com , but does not seem to have any effect

With idTcpServer1.Bindings.add do
 Begin
  Ip   := '10.10.0.22';
  Port := 6060;
 end;

This code makes sence , the add method adds a TIdSocketHandle instance to a collection of TIdSockethandles , wich are the bindings for the server.

Any help would be appreciated !

Thanx in advance.
EngwiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

smurffCommented:
if you break after and inspect this what do you see?

idTcpServer1.Bindings[0].port

0
smurffCommented:
Make sure you are setting the active after the bindings e.g.

procedure TForm1.Button1Click(Sender: TObject);
begin
  IdTCPServer1.Bindings.Clear;
  with IdTCPServer1.Bindings.Add do
  begin
    IP := GStack.LocalAddress;
    Port := 6060;
  end;
  IdTCPServer1.Active := True;
  Button1.Enabled := not IdTCPServer1.Active;
end;

Regards
Smurff
0
EngwiAuthor Commented:
Smurff

Yes , strangely enough I checked the value of the port number during runtime after setting it to the desired value and yes , the property gets modified but my client refuses to connect.

If I take that same value and set it via the Object inspector during design time , it works perfectly.

Any Ideas ??
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

smurffCommented:
Hmmm seems funny. I dont have Delphi installed here at the moment. Do you have the Indy examples? If so then do a search through the files for "bindings" then see what the examples do. Maybe even try a few of those with your connections detials and see if they fail....

In the mean time run your exe and run a command prompt (cmd.exe for WinNt or 2000) or (command.exe for win9x) and type

netstat -a

You should see something like :

TCP <your hostname>:6060 ...........blah blah LISTENING

If not then try building an exe with the settings from the object inspector and try the netstat again and compare.

Regards
Smurff
0
EngwiAuthor Commented:
Smurff

Thanx for the reply , tried that , here are the results :

With the object inspector , set the DEFAULTPORT to 7070 :

TCP      engwi:7070        engwi:0           LISTENING

Value set at runtime to 6060 with an Object inspector DEFAULTPORT value set to 7070
remains :

TCP      engwi:7070        engwi:0           LISTENING

Any thoughts on that ?



0
smurffCommented:
So are you saying that even though with the "runtime" set to 6060, your still seeing the 7070 BUT NOT 6060 ?

If thats the case then its not binding at all.
Theres a PC with Delphi on in the other building, Im going over there at 3:30 GMT so I`ll have a test there.

Regards
Smurff
0
EngwiAuthor Commented:
Smurff

Thanx for your effort ! I really appreciate it , it's 1:30 GMT here now so I will be waiting.

Thanx again.
0
smurffCommented:
Engwi,

I have tried the code I put before and it worked fine.


begin
 IdTCPServer1.Bindings.Clear;
 with IdTCPServer1.Bindings.Add do
 begin
   IP := '127.0.0.1';
   Port := 6060;
 end;
 IdTCPServer1.Active := True;
 Button1.Enabled := not IdTCPServer1.Active;
end;

Then I ran this command line e.g.

goto run, type cmd.exe then in the command box type

telnet 127.0.0.1 6060

Delphi will bring up an exception complaining about handlers etc etc but you know you have connected. Try another real high port number like 30000 and try that maybe.

Just try a new application and drop your idtcpserver on there then put the code above in a button. Run in IDE and press the button. Then do the telnet thing above and see what happens.

Regards
Smurff
0
EngwiAuthor Commented:
Smurff,

Yes , the code works fine , I am able to connect but why dont you try an simulate my problem.

Set a port value in the object inspector. Run your server.
Telnet to that ip address and port. Fine  , you get connected.

Now , via code , NOT the object inspector , change the default port to something other than the one spesified in the Object inspector.

Run your server.

Try to connect to the server using that port you spesified in the code.

Please see if you are able to recreate my problem.

Thanx again.
0
smurffCommented:
In the object inspector I put 7777 and run the exe. I then pressed on the button that had my code where it set the port to 6060 then I could connect fine.

What version of indy are you using? I have the latest.
but I tried to replicate it and it works fine for me.

maybe you could add : IdTCPServer1.DefaultPort := 0;

begin
IdTCPServer1.DefaultPort := 0;
IdTCPServer1.Bindings.Clear;
with IdTCPServer1.Bindings.Add do
begin
  IP := '127.0.0.1';
  Port := 6060;
end;
IdTCPServer1.Active := True;
Button1.Enabled := not IdTCPServer1.Active;
end;

If this above code works then use that, I mean are you setting the object inspector default port then hoping to change it during run time? .. If not then just use that.
Regards
Smurff
0
EngwiAuthor Commented:
Smurff

Thanx for putting up with my stupidity. Yes , all works fine now , still cant pinpoint the problem with my code but it works !! Thanx again.

I will increase the points.

Thanx again !!

0
EngwiAuthor Commented:
Smurff

This seems to be my problem. I am using the startup method for a WinNt service application to init the TCPservice.

When starting the service in the service control manager and attempting to connect , it fails me.

When using that same code in a normal application all is fine.

Any particular reason why this would happen ??
0
smurffCommented:
ahh you didnt say anything about in a service :) Send me the whole source code to

Danny.Kellett@_remedy.com take out the _ after @ (to stop spammers)

And I`ll have a look
Regards
Smurff
0
smurffCommented:
Try this.
1)File -> New -> Other -> Service Application
2)Drop an idTcpServer
3)Click on the Object Treeview and click on the Service1
4)Then look at the object inspector and goto the tab named Events and double click the OnStart then put the code as before, less the button stuff ;)
5)Save and build it
6)Run it once with -install to install the service. Make sure your an local administrator
7)start the service and try to connect via telnet as before

You will see something like :

"Connection to host lost"

This is because we have no handlers within indy but this does mean we are connecting. If it waits for a while and says something like
"could not open a connection ..." then that means this failed.

Try something like that.
Regards
Smurff
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
EngwiAuthor Commented:
Smurff

Got your results , "Connection to host lost"

What do you suggest , I try another route for my TCPServer other than creating a Winnt service application to host my tcp server ?
0
EngwiAuthor Commented:
Smurff

I must be cursed or something. After following your instructions and rebuilding my service application , all is well and the TPCServer acts on the settings supplied during runtime.

Thanx again.

Regards
 Engwi
0
smurffCommented:
>Got your results , "Connection to host lost"
Good, that means you connected. The only reason we got that message was because we didnt set the indy component up correctly, I just said to that just to prove the bindings were working and that you could connect.

Creating an NT Service the way I showed has worked for me loads of times and to date .... every component I have put in there works :)

Good, glad I could help.

Regards
Smurff
0
smurffCommented:
thanks :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.