Sample code for 30+ languages & platforms
Delphi DLL

Upgrade a Plain TCP Connection to TLS

See more Socket/SSL/TLS Examples

Demonstrates Socket.ConvertToSsl, which upgrades an already-connected plain TCP socket to TLS by performing a client-side TLS handshake over the existing connection.

Background. This supports STARTTLS-style upgrades, where a protocol begins in plaintext and switches to TLS at a defined point. Perform the protocol-specific negotiation first, then call ConvertToSsl.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
socket: HCkSocket;
bTls: Boolean;
maxWaitMs: Integer;

begin
success := False;

socket := CkSocket_Create();

//  Connect as plain TCP (no TLS yet).
bTls := False;
maxWaitMs := 5000;
success := CkSocket_Connect(socket,'example.com',5000,bTls,maxWaitMs);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSocket__lastErrorText(socket));
    Exit;
  end;

//  At the point where the application protocol permits a STARTTLS-style upgrade, perform any required
//  negotiation, then upgrade the existing connection to TLS with a client-side handshake.
success := CkSocket_ConvertToSsl(socket);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSocket__lastErrorText(socket));
    Exit;
  end;
Memo1.Lines.Add('Connection upgraded to TLS.');

CkSocket_Dispose(socket);