Recent

Author Topic: [SOLVED] check that a intra net server listens to a TCP socket  (Read 1918 times)

guest58172

  • Guest
[SOLVED] check that a intra net server listens to a TCP socket
« on: February 08, 2016, 03:32:12 pm »
Under windows I have to wait until a local server starts to listen to a particular TCP port.
To do so:

Code: Pascal  [Select][+][-]
  1. function checkSocket: boolean;
  2. var
  3.   prt: word = 9166;
  4.   cli: TInetSocket = nil;
  5. begin
  6.   sleep(100); // because this proc is looped
  7.   result := false;
  8.   try try
  9.     cli := TInetSocket.Create('localhost', prt);
  10.   except
  11.     // fail to connect error interpreted as "the server occupies the port"
  12.     result := true;
  13.   end;
  14.   finally
  15.     if assigned(cli) then
  16.       cli.Free;
  17.   end;
  18. end;

It seems to work however once it returns true, the server fails to create its connection to the socket because it looks like my checker blocks the port.

What's the proper way to check if a port is available or not ?

balazsszekely

  • Guest
Re: check that a intra net server listens to a TCP socket
« Reply #1 on: February 08, 2016, 04:03:23 pm »
Run with TProcess "netstat -a" then parse the result.

guest58172

  • Guest
Re: check that a intra net server listens to a TCP socket
« Reply #2 on: February 08, 2016, 04:07:15 pm »
Yeah, this is what I gonna do. I'll wait a bit before considering this Q as solved to see if anyone has a solution using a socket, which would be cleaner.

balazsszekely

  • Guest
Re: check that a intra net server listens to a TCP socket
« Reply #3 on: February 08, 2016, 04:28:00 pm »
Maybe this helps:
Code: Pascal  [Select][+][-]
  1. cli.SocketOptions:= cli.SocketOptions - [soKeepAlive];
Alternatively you could use the closesocket api(https://msdn.microsoft.com/en-us/library/windows/desktop/ms737582%28v=vs.85%29.aspx).

 

TinyPortal © 2005-2018