Vert.x系列:
Vert.x介绍:https://blog.csdn.net/haoranhaoshi/article/details/89279096
Vert.x实战一:Vert.x通过Http发布数据:https://blog.csdn.net/haoranhaoshi/article/details/89284847
Vert.x实战二:TCP通信:https://blog.csdn.net/haoranhaoshi/article/details/89296522
Vert.x实战三:TCP客户端之间以角色通过服务端转接通信:https://mp.csdn.net/postedit/89296606
Vert.x实战四:TCP客户端之间以角色和同一角色连接顺序通过服务端转接通信:https://blog.csdn.net/haoranhaoshi/article/details/89296665
Vert.x实战五:TCP客户端之间以ID通过服务端转接通信:https://blog.csdn.net/haoranhaoshi/article/details/89296754
Vert.x实战六:TCP客户端之间以功能名通过服务端转接通信:https://blog.csdn.net/haoranhaoshi/article/details/89296841
Vert.x实战七:TCP设置超时断开:https://blog.csdn.net/haoranhaoshi/article/details/89296986
Vert.x的TCP服务端和客户端配置:https://blog.csdn.net/haoranhaoshi/article/details/89297022
Vert.x的Http和TCP实战代码下载:https://download.csdn.net/download/haoranhaoshi/11114611
本篇:TCP默认无连接超时断开,无每2小时保活。但都可设置。TCP服务端的超时设置对所有Socket连接起效。超时设置是服务端和客户端超时设置的最小值。运行如下示例可知。
package VertxTCPIdleTimeTset;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.net.NetServer;
import io.vertx.core.net.NetServerOptions;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class VertxTCPServer extends AbstractVerticle {
@Override
public void start() {
NetServerOptions options = new NetServerOptions();
// 启动TCP保活,默认不保活
options.setTcpKeepAlive(true);
options.setIdleTimeout(5);
options.isTcpQuickAck();
// 创建TCP服务器
NetServer server = vertx.createNetServer(options);
// 处理连接请求
server.connectHandler(socket -> {
Date connectDate = new Date();
SimpleDateFormat connectSimpleDateFormat = new SimpleDateFormat ("a hh:mm:ss");
System.out.println("客户端连接时间:" + connectSimpleDateFormat.format(connectDate));
socket.handler(buffer -> {
// 在这里应该解析报文,封装为协议对象,并找到响应的处理类,得到处理结果,并响应
System.out.println("接收到的数据为:" + buffer.toString());
});
// 监听客户端的退出连接
socket.closeHandler(close -> {
Date closeDate = new Date();
SimpleDateFormat closeSimpleDateFormat = new SimpleDateFormat ("a hh:mm:ss");
System.out.println("客户端退出连接时间:" + closeSimpleDateFormat.format(closeDate));
System.out.println("客户端连接时长:" + (closeDate.getTime() - connectDate.getTime()) / 1000.0 + "秒");
});
});
// 监听端口
server.listen(33323, res -> {
if (res.succeeded()) {
System.out.println("服务器启动成功");
}
});
}
public static void main(String[] args) {
Vertx.vertx().deployVerticle(new VertxTCPServer());
}
}
package VertxTCPIdleTimeTset;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.net.NetClient;
import io.vertx.core.net.NetClientOptions;
import io.vertx.core.net.NetSocket;
import java.util.Scanner;
public class VertxTCPClient extends AbstractVerticle {
private static NetSocket netSocket;
@Override
public void start() {
NetClientOptions options = new NetClientOptions();
// 未连接则重新连接的间隔和尝试次数,重连尝试次数默认为0次,重连间隔默认为1000ms
options.setTcpKeepAlive(true);
options.setReconnectAttempts(10);
options.setReconnectInterval(500);
// 空闲超时options.getIdleTimeout()默认为0,单位options.getIdleTimeoutUnit()默认为s,可设置:options.setIdleTimeoutUnit(TimeUnit.SECONDS);
options.setIdleTimeout(10);
// 创建一个TCP客户端
NetClient client = vertx.createNetClient(options);
// 连接服务器
client.connect(33323, "localhost", conn -> {
if (conn.succeeded()) {
System.out.println("客户端连接服务端成功");
netSocket = conn.result();
// 读取服务器的响应数据
netSocket.handler(buffer -> {
System.out.println("接收到的数据为:" + buffer.toString());
});
} else {
System.out.println("连接服务器异常");
}
});
}
public static void main(String[] args) {
Vertx.vertx().deployVerticle(new VertxTCPClient());
// 向服务端发送消息
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String message = scanner.next();
netSocket.write(Buffer.buffer(message));
}
}
}