How to verify if it's possible to connect to a TCP server ?
To your first question; definitely wrap the connection attempt to a separate thread, which you'll run when your splash screen shows. In that thread you can simply try to Connect
and catch the exception. If the exception is raised, the connection failed. If not, you were able to connect. For the notification about this state I would use custom messages, which you'll send to a splash screen form like shown in the following pseudocode:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdTCPClient;
const
WM_CONNECTION_NOTIFY = WM_USER + 1;
SC_CONNECTION_FAILURE = 0;
SC_CONNECTION_SUCCESS = 1;
type
TConnThread = class(TThread)
private
FMsgHandler: HWND;
FTCPClient: TIdTCPClient;
protected
procedure Execute; override;
public
constructor Create(const AHost: string; APort: Word; ATimeout: Integer;
AMsgHandler: HWND); reintroduce;
destructor Destroy; override;
end;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
FConnThread: TConnThread;
procedure WMConnectionNotify(var AMessage: TMessage); message WM_CONNECTION_NOTIFY;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TConnThread }
constructor TConnThread.Create(const AHost: string; APort: Word;
ATimeout: Integer; AMsgHandler: HWND);
begin
inherited Create(False);
FreeOnTerminate := False;
FMsgHandler := AMsgHandler;
FTCPClient := TIdTCPClient.Create(nil);
FTCPClient.Host := AHost;
FTCPClient.Port := APort;
FTCPClient.ConnectTimeout := ATimeout;
end;
destructor TConnThread.Destroy;
begin
FTCPClient.Free;
inherited;
end;
procedure TConnThread.Execute;
begin
try
FTCPClient.Connect;
PostMessage(FMsgHandler, WM_CONNECTION_NOTIFY, 0, SC_CONNECTION_SUCCESS);
except
PostMessage(FMsgHandler, WM_CONNECTION_NOTIFY, 0, SC_CONNECTION_FAILURE);
end;
end;
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
FConnThread := TConnThread.Create('123.4.5.6', 123, 5000, Handle);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FConnThread.Free;
end;
procedure TForm1.WMConnectionNotify(var AMessage: TMessage);
begin
case AMessage.LParam of
// the connection failed
SC_CONNECTION_FAILURE: ;
// the connection succeeded
SC_CONNECTION_SUCCESS: ;
end;
end;
end.
Does the server need to repeatedly broadcast some sort of message to inform potential clients that is running ?
No, this works in a different direction - client asks server if it's running. It's like that simply because server doesn't know clients, but client knows server.