Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oSocket
LOCAL nBTls
LOCAL nMaxWaitMs

nSuccess := 0

oSocket := CreateObject("Chilkat.Socket")

//  Connect as plain TCP (no TLS yet).
nBTls := 0
nMaxWaitMs := 5000
nSuccess := oSocket:Connect("example.com", 5000, nBTls, nMaxWaitMs)
IF (nSuccess == 0)
    ? oSocket:LastErrorText
    oSocket:destroy()
    RETURN
ENDIF

//  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.
nSuccess := oSocket:ConvertToSsl()
IF (nSuccess == 0)
    ? oSocket:LastErrorText
    oSocket:destroy()
    RETURN
ENDIF

? "Connection upgraded to TLS."

oSocket:destroy()