TsgcTCPClient › Methods › Connect

Connect Method

Opens the TCP connection synchronously and blocks the caller until the socket is connected or the timeout elapses.

Syntax

function Connect(const aTimeout: Integer = 10000): Boolean;

Parameters

NameTypeDescription
aTimeoutconst IntegerMaximum time in milliseconds to wait for the connection to become active. Defaults to 10000 (10 seconds).

Return Value

True when the TCP socket has been established within the timeout; False otherwise. (Boolean)

Remarks

Connect is the blocking counterpart to setting Active := True. It sets Active internally and then waits on an internal event until the TCP handshake completes or the timeout expires. Unlike the WebSocket client there is no HTTP upgrade step, so the method returns as soon as the raw socket (and the TLS negotiation, when enabled) is established. If the client is already connected, the method returns True immediately. When the function returns True it is safe to call WriteData straight away, without waiting for OnConnect. Use Start instead if you need a non-blocking call that connects from a secondary thread.

Example


oClient := TsgcTCPClient.Create(nil);
oClient.Host := '127.0.0.1';
oClient.Port := 5000;
if oClient.Connect(5000) then
  oClient.WriteData('Hello Server')
else
  ShowMessage('Connection failed');

Back to Methods