?
Solved

Delphi Indy TCPServer bindings at runtime

Posted on 2003-03-19
18
Medium Priority
?
4,646 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:Engwi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 8
18 Comments
 
LVL 3

Expert Comment

by:smurff
ID: 8165673
if you break after and inspect this what do you see?

idTcpServer1.Bindings[0].port

0
 
LVL 3

Expert Comment

by:smurff
ID: 8165686
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
 

Author Comment

by:Engwi
ID: 8165950
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Expert Comment

by:smurff
ID: 8166129
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
 

Author Comment

by:Engwi
ID: 8166310
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
 
LVL 3

Expert Comment

by:smurff
ID: 8166507
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
 

Author Comment

by:Engwi
ID: 8166577
Smurff

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

Thanx again.
0
 
LVL 3

Expert Comment

by:smurff
ID: 8167551
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
 

Author Comment

by:Engwi
ID: 8167624
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
 
LVL 3

Expert Comment

by:smurff
ID: 8167815
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
 

Author Comment

by:Engwi
ID: 8167899
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
 

Author Comment

by:Engwi
ID: 8167993
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
 
LVL 3

Expert Comment

by:smurff
ID: 8168160
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
 
LVL 3

Accepted Solution

by:
smurff earned 150 total points
ID: 8168236
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
 

Author Comment

by:Engwi
ID: 8168452
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
 

Author Comment

by:Engwi
ID: 8172070
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
 
LVL 3

Expert Comment

by:smurff
ID: 8172467
>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
 
LVL 3

Expert Comment

by:smurff
ID: 8348596
thanks :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month12 days, 10 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question