Client Example

import std.socket.TcpSocket
import crypto.x509.{X509Certificate,PrivateKey}
import net.tls.*

main() {
    var config = TlsClientConfig()
    config.verifyMode = TrustAll
    config.alpnProtocolsList = ["h2"]

    // Used to restore a session.
    var lastSession: ?TlsSession = None

    while (true) {// Reconnects the loop.
        try (socket = TcpSocket("127.0.0.1", 8443)) {
            socket.connect() // Establishes the TCP connection.
            try (tls = TlsSocket.client(socket, clientConfig: config, session: lastSession)) {
                try {
                    tls.handshake()  // then we are negotiating TLS
                    lastSession = tls.session // If the negotiation is successful, the TLS session is stored for next reconnection.
                } catch (e: Exception) {
                    lastSession = None // If the negotiation fails, the session is deleted.
                    throw e
                }

               // The TLS instance is complete.
               tls.write("Hello, peer! Let's discuss our personal secrets.\n".toArray())
            }
        } catch (e: Exception) {
            println("client connection failed ${e}, retrying...")
        }
    }
}