Testing functionalitys with indy 10, i developed a very small app to detect if the local ports are opened to receive comunications:
EDITED CODE: Now it is working.
procedure TForm1.Button1Click(Sender: TObject);
var
contador, Start, Finish : Integer;
IP : String;
Portsfound : integer = 0;
MyServer1 : TIdTCPServer;
begin
MyServer1 := TIdTCPServer.Create(form1);
Myserver1.OnExecute:=@form1.IdTCPServer1Execute;
IP := Edit1.Text;
try
Start := StrToInt(Edit2.Text);
Finish := StrToInt(Edit3.Text);
Except on E:exception do
Begin
memo1.lines.Add('Invalid parameters');
exit;
end;
end;
for contador := start to finish do
begin
label4.Caption:='Ports found: '+IntToStr(Portsfound);
application.ProcessMessages;
MyServer1.Bindings.Clear;
MyServer1.DefaultPort:=contador;
try
MyServer1.Active:=true;
memo1.lines.Add('Port available to listent= '+IntToStr(contador));
Except on E:Exception do
begin
memo1.lines.Add('Unable to listen to port= '+IntToStr(contador));
Continue;
end;
end;
if testcliente(ip,contador) then Portsfound := Portsfound+1;
MyServer1.Active:=false;
end;
end;
function TForm1.testcliente(Ip:String;port:Integer):boolean;
var
mycliente : TIdTCPClient;
begin
result := true;
mycliente := TIdTCPClient.Create(form1);
mycliente.host := ip;
mycliente.Port:=port;
mycliente.ConnectTimeout:=200;
memo1.lines.Add('Connecting to '+Ip+':'+IntToStr(port));
try
mycliente.Connect;
memo1.lines.Add('Connected to port= '+IntToStr(port));
mycliente.Disconnect;
except on E:Exception do
begin
memo1.lines.Add('Unable to connect to port= '+IntToStr(port)+' '+E.Message);
result := false
end;
end;
mycliente.free;
end;
Example values: IP:= (The user public IP) - Start:= 8005 - Finish := 8010
As a result, the app always can connect ONLY to the first tested port; the server can listen to all ports, but the client is only able to connect to the first one. Seems like something is not freed correctly after first loop, but i can not find what.
Thanks you.