## Writing TCP servers and clients

Vert.x allows you to easily write non blocking TCP clients and servers.

### Creating a TCP server

The simplest way to create a TCP server, using all default options is as follows:

    var server = vertx.createNetServer()

### Configuring a TCP server

If you don’t want the default, a server can be configured by passing in a `NetServerOptions` instance when creating it:

    var options = NetServerOptions()
      .setPort(4321)

    var server = vertx.createNetServer(options)

### Start the Server Listening

To tell the server to listen for incoming requests you use one of the `listen` alternatives.

To tell the server to listen at the host and port as specified in the options:

    var server = vertx.createNetServer()
    server.listen()

Or to specify the host and port in the call to listen, ignoring what is configured in the options:

    var server = vertx.createNetServer()
    server.listen(1234, "localhost")

The default host is `0.0.0.0` which means 'listen on all available addresses' and the default port is `0`, which is a special value that instructs the server to find a random unused local port and use that.

The actual bind is asynchronous so the server might not actually be listening until some time after the call to listen has returned.

If you want to be notified when the server is actually listening you can provide a handler to the `listen` call. For example:

    var server = vertx.createNetServer()
    server.listenFuture(1234, "localhost").onComplete{
      case Success(result) => {
        println("Server is now listening!")
      }
      case Failure(cause) => {
        println(s"$cause")
      }
    }

### Listening on a random port

If `0` is used as the listening port, the server will find an unused random port to listen on.

To find out the real port the server is listening on you can call `actualPort`.

    var server = vertx.createNetServer()
    server.listenFuture(0, "localhost").onComplete{
      case Success(result) => {
        println(s"Server is now listening on actual port: ${server.actualPort()}")
      }
      case Failure(cause) => {
        println(s"$cause")
      }
    }

### Getting notified of incoming connections

To be notified when a connection is made you need to set a `connectHandler`:

    var server = vertx.createNetServer()
    server.connectHandler((socket: io.vertx.scala.core.net.NetSocket) => {
      // Handle the connection in here
    })

When a connection is made the handler will be called with an instance of `NetSocket`.

This is a socket-like interface to the actual connection, and allows you to read and write data as well as do various other things like close the socket.

### Reading data from the socket

To read data from the socket you set the `handler` on the socket.

This handler will be called with an instance of `Buffer` every time data is received on the socket.

    var server = vertx.createNetServer()
    server.connectHandler((socket: io.vertx.scala.core.net.NetSocket) => {
      socket.handler((buffer: io.vertx.scala.core.buffer.Buffer) => {
        println(s"I received some bytes: ${buffer.length()}")
      })
    })

### Writing data to a socket

You write to a socket using one of `write`.

    // Write a buffer
    var buffer = Buffer.buffer().appendFloat(12.34).appendInt(123)
    socket.write(buffer)

    // Write a string in UTF-8 encoding
    socket.write("some data")

    // Write a string using the specified encoding
    socket.write("some data", "UTF-16")

Write operations are asynchronous and may not occur until some time after the call to write has returned.

### Closed handler

If you want to be notified when a socket is closed, you can set a `closeHandler` on it:

    socket.closeHandler((v: java.lang.Void) => {
      println("The socket has been closed")
    })

### Handling exceptions

You can set an `exceptionHandler` to receive any exceptions that happen on the socket.

### Event bus write handler

Every socket automatically registers a handler on the event bus, and when any buffers are received in this handler, it writes them to itself.

This enables you to write data to a socket which is potentially in a completely different verticle or even in a different Vert.x instance by sending the buffer to the address of that handler.

The address of the handler is given by `writeHandlerID`

### Local and remote addresses

The local address of a `NetSocket` can be retrieved using `localAddress`.

The remote address, (i.e. the address of the other end of the connection) of a `NetSocket` can be retrieved using `remoteAddress`.

### Sending files or resources from the classpath

Files and classpath resources can be written to the socket directly using `sendFile`. This can be a very efficient way to send files, as it can be handled by the OS kernel directly where supported by the operating system.

Please see the chapter about serving files from the classpath for restrictions of the classpath resolution or disabling it.

    socket.sendFile("myfile.dat")

### Streaming sockets

Instances of `NetSocket` are also `ReadStream` and `WriteStream` instances so they can be used to pump data to or from other read and write streams.

See the chapter on streams and pumps for more information.

### Upgrading connections to SSL/TLS

A non SSL/TLS connection can be upgraded to SSL/TLS using `upgradeToSsl`.

The server or client must be configured for SSL/TLS for this to work correctly. Please see the chapter on SSL/TLS for more information.

### Closing a TCP Server

Call `close` to close the server. Closing the server closes any open connections and releases all server resources.

The close is actually asynchronous and might not complete until some time after the call has returned. If you want to be notified when the actual close has completed then you can pass in a handler.

This handler will then be called when the close has fully completed.

    server.closeFuture().onComplete{
      case Success(result) => {
        println("Server is now closed")
      }
      case Failure(cause) => {
        println(s"$cause")
      }
    }

### Automatic clean-up in verticles

If you’re creating TCP servers and clients from inside verticles, those servers and clients will be automatically closed when the verticle is undeployed.

### Scaling - sharing TCP servers

The handlers of any TCP server are always executed on the same event loop thread.

This means that if you are running on a server with a lot of cores, and you only have this one instance deployed then you will have at most one core utilised on your server.

In order to utilise more cores of your server you will need to deploy more instances of the server.

You can instantiate more instances programmatically in your code:

    // Create a few instances so we can utilise cores

    for ( i <- 0 until 10) {
      var server = vertx.createNetServer()
      server.connectHandler((socket: io.vertx.scala.core.net.NetSocket) => {
        socket.handler((buffer: io.vertx.scala.core.buffer.Buffer) => {
          // Just echo back the data
          socket.write(buffer)
        })
      })
      server.listen(1234, "localhost")
    }

or, if you are using verticles you can simply deploy more instances of your server verticle by using the `-instances` option on the command line:

    vertx run com.mycompany.MyVerticle -instances 10

or when programmatically deploying your verticle

    var options = DeploymentOptions()
      .setInstances(10)

    vertx.deployVerticle("com.mycompany.MyVerticle", options)

Once you do this you will find the echo server works functionally identically to before, but all your cores on your server can be utilised and more work can be handled.

At this point you might be asking yourself 'How can you have more than one server listening on the same host and port? Surely you will get port conflicts as soon as you try and deploy more than one instance?'

Vert.x does a little magic here.*

When you deploy another server on the same host and port as an existing server it doesn’t actually try and create a new server listening on the same host/port.

Instead it internally maintains just a single server, and, as incoming connections arrive it distributes them in a round-robin fashion to any of the connect handlers.

Consequently Vert.x TCP servers can scale over available cores while each instance remains single threaded.

### Creating a TCP client

The simplest way to create a TCP client, using all default options is as follows:

    var client = vertx.createNetClient()

### Configuring a TCP client

If you don’t want the default, a client can be configured by passing in a `NetClientOptions` instance when creating it:

    var options = NetClientOptions()
      .setConnectTimeout(10000)

    var client = vertx.createNetClient(options)

### Making connections

To make a connection to a server you use `connect`, specifying the port and host of the server and a handler that will be called with a result containing the `NetSocket` when connection is successful or with a failure if connection failed.

    var options = NetClientOptions()
      .setConnectTimeout(10000)

    var client = vertx.createNetClient(options)
    client.connectFuture(4321, "localhost").onComplete{
      case Success(result) => {
        println("Connected!")
        var socket = result
      }
      case Failure(cause) => {
        println(s"$cause")
      }
    }

### Configuring connection attempts

A client can be configured to automatically retry connecting to the server in the event that it cannot connect. This is configured with `reconnectInterval` and `reconnectAttempts`.

Note | Currently Vert.x will not attempt to reconnect if a connection fails, reconnect attempts and interval only apply to creating initial connections.

    var options = NetClientOptions()
      .setReconnectAttempts(10)
      .setReconnectInterval(500)

    var client = vertx.createNetClient(options)

By default, multiple connection attempts are disabled.

### Logging network activity

For debugging purposes, network activity can be logged:

    var options = NetServerOptions()
      .setLogActivity(true)

    var server = vertx.createNetServer(options)

for the client

    var options = NetClientOptions()
      .setLogActivity(true)

    var client = vertx.createNetClient(options)

Network activity is logged by Netty with the `DEBUG` level and with the `io.netty.handler.logging.LoggingHandler` name. When using network activity logging there are a few things to keep in mind:

- logging is not performed by Vert.x logging but by Netty

- this is not a production feature

You should read the [netty-logging] section.

### Configuring servers and clients to work with SSL/TLS

TCP clients and servers can be configured to use Transport Layer Security - earlier versions of TLS were known as SSL.

The APIs of the servers and clients are identical whether or not SSL/TLS is used, and it’s enabled by configuring the `NetClientOptions` or `NetServerOptions` instances used to create the servers or clients.

#### Enabling SSL/TLS on the server

SSL/TLS is enabled with `ssl`.

By default it is disabled.

#### Specifying key/certificate for the server

SSL/TLS servers usually provide certificates to clients in order verify their identity to clients.

Certificates/keys can be configured for servers in several ways:

The first method is by specifying the location of a Java key-store which contains the certificate and private key.

Java key stores can be managed with the keytool utility which ships with the JDK.

The password for the key store should also be provided:

    var options = NetServerOptions()
      .setSsl(true)
      .setKeyStoreOptions(JksOptions()
        .setPath("/path/to/your/server-keystore.jks")
        .setPassword("password-of-your-keystore")
      )

    var server = vertx.createNetServer(options)

Alternatively you can read the key store yourself as a buffer and provide that directly:

    var myKeyStoreAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/server-keystore.jks")
    var jksOptions = JksOptions()
      .setValue(myKeyStoreAsABuffer)
      .setPassword("password-of-your-keystore")

    var options = NetServerOptions()
      .setSsl(true)
      .setKeyStoreOptions(jksOptions)

    var server = vertx.createNetServer(options)

Key/certificate in PKCS#12 format (http://en.wikipedia.org/wiki/PKCS_12), usually with the `.pfx` or the `.p12` extension can also be loaded in a similar fashion than JKS key stores:

    var options = NetServerOptions()
      .setSsl(true)
      .setPfxKeyCertOptions(PfxOptions()
        .setPath("/path/to/your/server-keystore.pfx")
        .setPassword("password-of-your-keystore")
      )

    var server = vertx.createNetServer(options)

Buffer configuration is also supported:

    var myKeyStoreAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/server-keystore.pfx")
    var pfxOptions = PfxOptions()
      .setValue(myKeyStoreAsABuffer)
      .setPassword("password-of-your-keystore")

    var options = NetServerOptions()
      .setSsl(true)
      .setPfxKeyCertOptions(pfxOptions)

    var server = vertx.createNetServer(options)

Another way of providing server private key and certificate separately using `.pem` files.

    var options = NetServerOptions()
      .setSsl(true)
      .setPemKeyCertOptions(PemKeyCertOptions()
        .setKeyPath("/path/to/your/server-key.pem")
        .setCertPath("/path/to/your/server-cert.pem")
      )

    var server = vertx.createNetServer(options)

Buffer configuration is also supported:

    var myKeyAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/server-key.pem")
    var myCertAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/server-cert.pem")
    var pemOptions = PemKeyCertOptions()
      .setKeyValue(myKeyAsABuffer)
      .setCertValue(myCertAsABuffer)

    var options = NetServerOptions()
      .setSsl(true)
      .setPemKeyCertOptions(pemOptions)

    var server = vertx.createNetServer(options)

Keep in mind that pem configuration, the private key is not crypted.

#### Specifying trust for the server

SSL/TLS servers can use a certificate authority in order to verify the identity of the clients.

Certificate authorities can be configured for servers in several ways:

Java trust stores can be managed with the keytool utility which ships with the JDK.

The password for the trust store should also be provided:

    var options = NetServerOptions()
      .setSsl(true)
      .setClientAuth(ClientAuth.REQUIRED)
      .setTrustStoreOptions(JksOptions()
        .setPath("/path/to/your/truststore.jks")
        .setPassword("password-of-your-truststore")
      )

    var server = vertx.createNetServer(options)

Alternatively you can read the trust store yourself as a buffer and provide that directly:

    var myTrustStoreAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/truststore.jks")
    var options = NetServerOptions()
      .setSsl(true)
      .setClientAuth(ClientAuth.REQUIRED)
      .setTrustStoreOptions(JksOptions()
        .setValue(myTrustStoreAsABuffer)
        .setPassword("password-of-your-truststore")
      )

    var server = vertx.createNetServer(options)

Certificate authority in PKCS#12 format (http://en.wikipedia.org/wiki/PKCS_12), usually with the `.pfx` or the `.p12` extension can also be loaded in a similar fashion than JKS trust stores:

    var options = NetServerOptions()
      .setSsl(true)
      .setClientAuth(ClientAuth.REQUIRED)
      .setPfxTrustOptions(PfxOptions()
        .setPath("/path/to/your/truststore.pfx")
        .setPassword("password-of-your-truststore")
      )

    var server = vertx.createNetServer(options)

Buffer configuration is also supported:

    var myTrustStoreAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/truststore.pfx")
    var options = NetServerOptions()
      .setSsl(true)
      .setClientAuth(ClientAuth.REQUIRED)
      .setPfxTrustOptions(PfxOptions()
        .setValue(myTrustStoreAsABuffer)
        .setPassword("password-of-your-truststore")
      )

    var server = vertx.createNetServer(options)

Another way of providing server certificate authority using a list `.pem` files.

    var options = NetServerOptions()
      .setSsl(true)
      .setClientAuth(ClientAuth.REQUIRED)
      .setPemTrustOptions(PemTrustOptions()
        .setCertPaths(Set("/path/to/your/server-ca.pem"))
      )

    var server = vertx.createNetServer(options)

Buffer configuration is also supported:

    var myCaAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/server-ca.pfx")
    var options = NetServerOptions()
      .setSsl(true)
      .setClientAuth(ClientAuth.REQUIRED)
      .setPemTrustOptions(PemTrustOptions()
        .setCertValues(Set(myCaAsABuffer))
      )

    var server = vertx.createNetServer(options)

#### Enabling SSL/TLS on the client

Net Clients can also be easily configured to use SSL. They have the exact same API when using SSL as when using standard sockets.

To enable SSL on a NetClient the function setSSL(true) is called.

#### Client trust configuration

If the `trustALl` is set to true on the client, then the client will trust all server certificates. The connection will still be encrypted but this mode is vulnerable to 'man in the middle' attacks. I.e. you can’t be sure who you are connecting to. Use this with caution. Default value is false.

    var options = NetClientOptions()
      .setSsl(true)
      .setTrustAll(true)

    var client = vertx.createNetClient(options)

If `trustAll` is not set then a client trust store must be configured and should contain the certificates of the servers that the client trusts.

By default, host verification is disabled on the client. To enable host verification, set the algorithm to use on your client (only HTTPS and LDAPS is currently supported):

    var options = NetClientOptions()
      .setSsl(true)
      .setHostnameVerificationAlgorithm("HTTPS")

    var client = vertx.createNetClient(options)

Likewise server configuration, the client trust can be configured in several ways:

The first method is by specifying the location of a Java trust-store which contains the certificate authority.

It is just a standard Java key store, the same as the key stores on the server side. The client trust store location is set by using the function `path` on the `jks options`. If a server presents a certificate during connection which is not in the client trust store, the connection attempt will not succeed.

    var options = NetClientOptions()
      .setSsl(true)
      .setTrustStoreOptions(JksOptions()
        .setPath("/path/to/your/truststore.jks")
        .setPassword("password-of-your-truststore")
      )

    var client = vertx.createNetClient(options)

Buffer configuration is also supported:

    var myTrustStoreAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/truststore.jks")
    var options = NetClientOptions()
      .setSsl(true)
      .setTrustStoreOptions(JksOptions()
        .setValue(myTrustStoreAsABuffer)
        .setPassword("password-of-your-truststore")
      )

    var client = vertx.createNetClient(options)

Certificate authority in PKCS#12 format (http://en.wikipedia.org/wiki/PKCS_12), usually with the `.pfx` or the `.p12` extension can also be loaded in a similar fashion than JKS trust stores:

    var options = NetClientOptions()
      .setSsl(true)
      .setPfxTrustOptions(PfxOptions()
        .setPath("/path/to/your/truststore.pfx")
        .setPassword("password-of-your-truststore")
      )

    var client = vertx.createNetClient(options)

Buffer configuration is also supported:

    var myTrustStoreAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/truststore.pfx")
    var options = NetClientOptions()
      .setSsl(true)
      .setPfxTrustOptions(PfxOptions()
        .setValue(myTrustStoreAsABuffer)
        .setPassword("password-of-your-truststore")
      )

    var client = vertx.createNetClient(options)

Another way of providing server certificate authority using a list `.pem` files.

    var options = NetClientOptions()
      .setSsl(true)
      .setPemTrustOptions(PemTrustOptions()
        .setCertPaths(Set("/path/to/your/ca-cert.pem"))
      )

    var client = vertx.createNetClient(options)

Buffer configuration is also supported:

    var myTrustStoreAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/ca-cert.pem")
    var options = NetClientOptions()
      .setSsl(true)
      .setPemTrustOptions(PemTrustOptions()
        .setCertValues(Set(myTrustStoreAsABuffer))
      )

    var client = vertx.createNetClient(options)

#### Specifying key/certificate for the client

If the server requires client authentication then the client must present its own certificate to the server when connecting. The client can be configured in several ways:

The first method is by specifying the location of a Java key-store which contains the key and certificate. Again it’s just a regular Java key store. The client keystore location is set by using the function `path` on the `jks options`.

    var options = NetClientOptions()
      .setSsl(true)
      .setKeyStoreOptions(JksOptions()
        .setPath("/path/to/your/client-keystore.jks")
        .setPassword("password-of-your-keystore")
      )

    var client = vertx.createNetClient(options)

Buffer configuration is also supported:

    var myKeyStoreAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/client-keystore.jks")
    var jksOptions = JksOptions()
      .setValue(myKeyStoreAsABuffer)
      .setPassword("password-of-your-keystore")

    var options = NetClientOptions()
      .setSsl(true)
      .setKeyStoreOptions(jksOptions)

    var client = vertx.createNetClient(options)

Key/certificate in PKCS#12 format (http://en.wikipedia.org/wiki/PKCS_12), usually with the `.pfx` or the `.p12` extension can also be loaded in a similar fashion than JKS key stores:

    var options = NetClientOptions()
      .setSsl(true)
      .setPfxKeyCertOptions(PfxOptions()
        .setPath("/path/to/your/client-keystore.pfx")
        .setPassword("password-of-your-keystore")
      )

    var client = vertx.createNetClient(options)

Buffer configuration is also supported:

    var myKeyStoreAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/client-keystore.pfx")
    var pfxOptions = PfxOptions()
      .setValue(myKeyStoreAsABuffer)
      .setPassword("password-of-your-keystore")

    var options = NetClientOptions()
      .setSsl(true)
      .setPfxKeyCertOptions(pfxOptions)

    var client = vertx.createNetClient(options)

Another way of providing server private key and certificate separately using `.pem` files.

    var options = NetClientOptions()
      .setSsl(true)
      .setPemKeyCertOptions(PemKeyCertOptions()
        .setKeyPath("/path/to/your/client-key.pem")
        .setCertPath("/path/to/your/client-cert.pem")
      )

    var client = vertx.createNetClient(options)

Buffer configuration is also supported:

    var myKeyAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/client-key.pem")
    var myCertAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/client-cert.pem")
    var pemOptions = PemKeyCertOptions()
      .setKeyValue(myKeyAsABuffer)
      .setCertValue(myCertAsABuffer)

    var options = NetClientOptions()
      .setSsl(true)
      .setPemKeyCertOptions(pemOptions)

    var client = vertx.createNetClient(options)

Keep in mind that pem configuration, the private key is not crypted.

#### Self-signed certificates for testing and development purposes

Caution | Do not use this in production settings, and note that the generated keys are very insecure.

It is very often the case that self-signed certificates are required, be it for unit / integration tests or for running a development version of an application.

`SelfSignedCertificate` can be used to provide self-signed PEM certificate helpers and give `KeyCertOptions` and `TrustOptions` configurations:

    var certificate = SelfSignedCertificate.create()

    var serverOptions = NetServerOptions()
      .setSsl(true)
      .setKeyCertOptions(certificate.keyCertOptions())
      .setTrustOptions(certificate.trustOptions())

    var server = vertx.createNetServer(serverOptions).connectHandler((socket: io.vertx.scala.core.net.NetSocket) => {
      socket.write("Hello!").end()
    }).listen(1234, "localhost")

    var clientOptions = NetClientOptions()
      .setSsl(true)
      .setKeyCertOptions(certificate.keyCertOptions())
      .setTrustOptions(certificate.trustOptions())

    var client = vertx.createNetClient(clientOptions)
    client.connectFuture(1234, "localhost").onComplete{
      case Success(result) => {
        result.handler((buffer: io.vertx.scala.core.buffer.Buffer) => {
          println(buffer)
        })
      }
      case Failure(cause) => {
        println(s"$cause")
      }
    }

The client can also be configured to trust all certificates:

    var clientOptions = NetClientOptions()
      .setSsl(true)
      .setTrustAll(true)

Note that self-signed certificates also work for other TCP protocols like HTTPS:

    var certificate = SelfSignedCertificate.create()

    vertx.createHttpServer(HttpServerOptions()
      .setSsl(true)
      .setKeyCertOptions(certificate.keyCertOptions())
      .setTrustOptions(certificate.trustOptions())
    ).requestHandler((req: io.vertx.scala.core.http.HttpServerRequest) => {
      req.response().end("Hello!")
    }).listen(8080)

#### Revoking certificate authorities

Trust can be configured to use a certificate revocation list (CRL) for revoked certificates that should no longer be trusted. The `crlPath` configures the crl list to use:

    var options = NetClientOptions()
      .setSsl(true)
      .setTrustStoreOptions(trustOptions)
      .setCrlPaths(Set("/path/to/your/crl.pem"))

    var client = vertx.createNetClient(options)

Buffer configuration is also supported:

    var myCrlAsABuffer = vertx.fileSystem().readFileBlocking("/path/to/your/crl.pem")
    var options = NetClientOptions()
      .setSsl(true)
      .setTrustStoreOptions(trustOptions)
      .setCrlValues(Set(myCrlAsABuffer))

    var client = vertx.createNetClient(options)

#### Configuring the Cipher suite

By default, the TLS configuration will use the Cipher suite of the JVM running Vert.x. This Cipher suite can be configured with a suite of enabled ciphers:

    var options = NetServerOptions()
      .setSsl(true)
      .setKeyStoreOptions(keyStoreOptions)
      .setEnabledCipherSuites(Set("ECDHE-RSA-AES128-GCM-SHA256", "ECDHE-ECDSA-AES128-GCM-SHA256", "ECDHE-RSA-AES256-GCM-SHA384", "CDHE-ECDSA-AES256-GCM-SHA384"))

    var server = vertx.createNetServer(options)

Cipher suite can be specified on the `NetServerOptions` or `NetClientOptions` configuration.

#### Configuring TLS protocol versions

By default, the TLS configuration will use the following protocol versions: SSLv2Hello, TLSv1, TLSv1.1 and TLSv1.2. Protocol versions can be configured by explicitly adding enabled protocols:

    var options = NetServerOptions()
      .setSsl(true)
      .setKeyStoreOptions(keyStoreOptions)
      .setEnabledSecureTransportProtocols(Set("TLSv1.1", "TLSv1.2"))

    var server = vertx.createNetServer(options)

Protocol versions can be specified on the `NetServerOptions` or `NetClientOptions` configuration.

#### SSL engine

The engine implementation can be configured to use OpenSSL instead of the JDK implementation. OpenSSL provides better performances and CPU usage than the JDK engine, as well as JDK version independence.

The engine options to use is

- the `getSslEngineOptions` options when it is set

- otherwise `JdkSSLEngineOptions`

    // Use JDK SSL engine
    var options = NetServerOptions()
      .setSsl(true)
      .setKeyStoreOptions(keyStoreOptions)

    // Use JDK SSL engine explicitly
    options = NetServerOptions()
      .setSsl(true)
      .setKeyStoreOptions(keyStoreOptions)
      .setJdkSslEngineOptions(JdkSSLEngineOptions())

    // Use OpenSSL engine
    options = NetServerOptions()
      .setSsl(true)
      .setKeyStoreOptions(keyStoreOptions)
      .setOpenSslEngineOptions(OpenSSLEngineOptions())

#### Server Name Indication (SNI)

Server Name Indication (SNI) is a TLS extension by which a client specifies an hostname attempting to connect: during the TLS handshake the clients gives a server name and the server can use it to respond with a specific certificate for this server name instead of the default deployed certificate.

When SNI is active the server uses

- the certificate CN or SAN DNS (Subject Alternative Name with DNS) to do an exact match, e.g `www.example.com`

- the certificate CN or SAN DNS certificate to match a wildcard name, e.g `*.example.com`

- otherwise the first certificate when the client does not present a server name or the presenter server name cannot be matched

You can enable SNI on the server by setting `sni` to `true` and configured the server with multiple key/certificate pairs.

Java KeyStore files or PKCS12 files can store multiple key/cert pairs out of the box.

    var keyCertOptions = JksOptions()
      .setPath("keystore.jks")
      .setPassword("wibble")

    var netServer = vertx.createNetServer(NetServerOptions()
      .setKeyStoreOptions(keyCertOptions)
      .setSsl(true)
      .setSni(true)
    )

`PemKeyCertOptions` can be configured to hold multiple entries:

    var keyCertOptions = PemKeyCertOptions()
      .setKeyPaths(List("default-key.pem", "host1-key.pem", "etc..."))
      .setCertPaths(List("default-cert.pem", "host2-key.pem", "etc..."))

    var netServer = vertx.createNetServer(NetServerOptions()
      .setPemKeyCertOptions(keyCertOptions)
      .setSsl(true)
      .setSni(true)
    )

The client implicitly sends the connecting host as an SNI server name for Fully Qualified Domain Name (FQDN).

You can provide an explicit server name when connecting a socket

    var client = vertx.createNetClient(NetClientOptions()
      .setTrustStoreOptions(trustOptions)
      .setSsl(true)
    )

    // Connect to 'localhost' and present 'server.name' server name
    client.connectFuture(1234, "localhost", "server.name").onComplete{
      case Success(result) => {
        println("Connected!")
        var socket = result
      }
      case Failure(cause) => {
        println(s"$cause")
      }
    }

It can be used for different purposes:

- present a server name different than the server host

- present a server name while connecting to an IP

- force to present a server name when using shortname

#### Application-Layer Protocol Negotiation (ALPN)

Application-Layer Protocol Negotiation (ALPN) is a TLS extension for application layer protocol negotiation. It is used by HTTP/2: during the TLS handshake the client gives the list of application protocols it accepts and the server responds with a protocol it supports.

Java 8 does not supports ALPN out of the box, so ALPN should be enabled by other means:

- OpenSSL support

- Jetty-ALPN support

The engine options to use is

- the `getSslEngineOptions` options when it is set

- `JdkSSLEngineOptions` when ALPN is available for JDK

- `OpenSSLEngineOptions` when ALPN is available for OpenSSL

- otherwise it fails

##### OpenSSL ALPN support

OpenSSL provides native ALPN support.

OpenSSL requires to configure `openSslEngineOptions` and use netty-tcnative jar on the classpath. Using tcnative may require OpenSSL to be installed on your OS depending on the tcnative implementation.

##### Jetty-ALPN support

Jetty-ALPN is a small jar that overrides a few classes of Java 8 distribution to support ALPN.

The JVM must be started with the alpn-boot-${version}.jar in its `bootclasspath`:

    -Xbootclasspath/p:/path/to/alpn-boot${version}.jar

where ${version} depends on the JVM version, e.g. 8.1.7.v20160121 for OpenJDK 1.8.0u74 . The complete list is available on the Jetty-ALPN page.

The main drawback is that the version depends on the JVM.

To solve this problem the Jetty ALPN agent can be use instead. The agent is a JVM agent that will chose the correct ALPN version for the JVM running it:

    -javaagent:/path/to/alpn/agent

### Using a proxy for client connections

The `NetClient` supports either a HTTP/1.x CONNECT, SOCKS4a or SOCKS5 proxy.

The proxy can be configured in the `NetClientOptions` by setting a `ProxyOptions` object containing proxy type, hostname, port and optionally username and password.

Here’s an example:

    var options = NetClientOptions()
      .setProxyOptions(ProxyOptions()
        .setType(ProxyType.SOCKS5)
        .setHost("localhost")
        .setPort(1080)
        .setUsername("username")
        .setPassword("secret")
      )

    var client = vertx.createNetClient(options)

The DNS resolution is always done on the proxy server, to achieve the functionality of a SOCKS4 client, it is necessary to resolve the DNS address locally.