Vert.x TCP Client
Jakob Jenkov |
Vert.x comes with a built-in TCP client that makes it easy to create TCP clients and work with them in asynchronous
mode. The Vert.x
TCP client class is called io.vertx.core.net.NetClient
.
Creating a TCP Client
You create a TCP client by creating an instance of NetClient
. You create an instance of NetClient
via the Vertx
object's method createNetClient()
. Here is how you create a NetClient
instance:
NetClient tcpClient = vertx.createNetClient();
Often you will create a NetClient
from inside a verticle. Here is how creating a Vert.x NetClient
from inside a verticle looks:
import io.vertx.core.AbstractVerticle; import io.vertx.core.net.NetClient; public class VertxTcpClientVerticle extends AbstractVerticle { public void start() { NetClient tcpClient = vertx.createNetClient(); } }
Connecting to a Remote Server
You connect to a remote server by calling the connect()
method. Here is how calling the connect()
method looks:
public class VertxTcpClientVerticle extends AbstractVerticle { public void start() { NetClient tcpClient = vertx.createNetClient(); tcpClient.connect(80, "jenkov.com", new Handler<AsyncResult<NetSocket>>(){ @Override public void handle(AsyncResult<NetSocket> result) { NetSocket socket = result.result(); } }); } }
You pass the TCP port of the remote server as well as the domain name, and a Handler
object which
is called when the connection is established. You obtain a reference to the NetSocket
connected
to the remote server via the AsyncResult
instance passed to the handler's handle()
method.
Writing Data
You can write data to the TCP connection via the NetSocket
write()
method. Here
is how that looks:
public class VertxTcpClientVerticle extends AbstractVerticle { public void start() { NetClient tcpClient = vertx.createNetClient(); tcpClient.connect(80, "jenkov.com", new Handler<AsyncResult<NetSocket>>(){ @Override public void handle(AsyncResult<NetSocket> result) { NetSocket socket = result.result(); socket.write("GET / HTTP/1.1\r\nHost: jenkov.com\r\n\r\n"); } }); } }
The write()
method is asynchronous and returns immediately. The data may not be sent by the time the
write()
method returns.
The Vert.x NetSocket
contains more versions of the write()
method which enables you
to write e.g. Buffer
s of data to the NetSocket
.
Reading Data
In order to read data from the NetSocket
you need to register a Handler
method on
the NetSocket
. Here is how you do register a Handler
on the NetSocket
:
public class VertxTcpClientVerticle extends AbstractVerticle { public void start() { NetClient tcpClient = vertx.createNetClient(); tcpClient.connect(80, "jenkov.com", new Handler<AsyncResult<NetSocket>>(){ @Override public void handle(AsyncResult<NetSocket> result) { NetSocket socket = result.result(); socket.write("GET / HTTP/1.1\r\nHost: jenkov.com\r\n\r\n"); socket.handler(new Handler<Buffer>(){ @Override public void handle(Buffer buffer) { System.out.println("Received data: " + buffer.length()); System.out.println(buffer.getString(0, buffer.length())); } }); } }); } }
The Handler
's handle()
method will get called when data is received from the remote server.
Closing the TCP Connection
Once you are finished using the TCP client you need to close it again. You close the TCP client by calling the
close()
method of the NetClient
instance. Here is how that looks:
tcpClient.close();
Again, the NetClient
's close()
method is asynchronous, so the underlying TCP connection
may not yet be closed by the time the close()
method returns.
Tweet | |
Jakob Jenkov |