Board index » delphi » Looking for a Port

Looking for a Port

Hi

I want to search for a free port.

I have placed a TSimpleServer on my form, and I'm trying to search for the
first free port between 80 and 100.

SimpleServer.BoundIP := '127.0.0.1';

for i := 80 to 100 do
begin
  SimpleServer.BoundPort := i;

  // what should I put here to test the port?
  try
    SimpleServer.Bind;  // generates an EIdCouldNotBindSocket if port is in
use.
                      // This is correct, but how can I just *test* the port
                      // without stopping program execution?
  except on EIdCouldNotBindSocket do {what?}
end;

Please reply soon.

Regards

Peter

 

Re:Looking for a Port


for I :=80 [...] do
[...]
try
 SimpleServer.Bind
except
end;

Now you need a futher check after the loop to see if the server is opened or
if every port failed.

If you're wondering why the program stops at the execution:
You have to turn of the "Stop at Delphi exceptions" checkbox at Tools/Debug
options/language options, then Delphi won't stop at every exception.
If you run the program outside of the IDE you won't notice the exceptions if
you catch them with a try..except statement.

Andy

Re:Looking for a Port


<<Andy:
 Now you need a futher check after the loop to see if the server is opened
or
 if every port failed.

Quote

Can I check if the server is opened by using.

If SimpleServer.Connected then
...
?

Regards

Peter

Re:Looking for a Port


I don't use Indy so someone else could better help you out there, but why
not just test it?

Andy

Other Threads