Link to home
Start Free TrialLog in
Avatar of Engwi
Engwi

asked on

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.
Avatar of smurff
smurff

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

idTcpServer1.Bindings[0].port

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
Avatar of Engwi

ASKER

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 ??
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
Avatar of Engwi

ASKER

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 ?



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
Avatar of Engwi

ASKER

Smurff

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

Thanx again.
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
Avatar of Engwi

ASKER

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.
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
Avatar of Engwi

ASKER

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 !!

Avatar of Engwi

ASKER

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 ??
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
ASKER CERTIFIED SOLUTION
Avatar of smurff
smurff

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Engwi

ASKER

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 ?
Avatar of Engwi

ASKER

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
>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
thanks :)