PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkSocket.pb"
Procedure ChilkatExample()
success.i = 0
socket.i = CkSocket::ckCreate()
If socket.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Connect as plain TCP (no TLS yet).
bTls.i = 0
maxWaitMs.i = 5000
success = CkSocket::ckConnect(socket,"example.com",5000,bTls,maxWaitMs)
If success = 0
Debug CkSocket::ckLastErrorText(socket)
CkSocket::ckDispose(socket)
ProcedureReturn
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.
success = CkSocket::ckConvertToSsl(socket)
If success = 0
Debug CkSocket::ckLastErrorText(socket)
CkSocket::ckDispose(socket)
ProcedureReturn
EndIf
Debug "Connection upgraded to TLS."
CkSocket::ckDispose(socket)
ProcedureReturn
EndProcedure