Vert.x实战七:TCP设置超时断开

news/2024/5/17 15:14:08 标签: Vert.x, TCP, Java

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.xTCP服务端和客户端配置: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));
        }
    }
}

 


http://www.niftyadmin.cn/n/752606.html

相关文章

【DFS】【递归】【Java】Leetcode 733. 图像渲染

有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 到 65535 之间。 给你一个坐标 (sr, sc) 表示图像渲染开始的像素值(行 ,列)和一个新的颜色值 newColor,让你重新上色这幅图像…

wpf 禁用window的systemmenu

private IntPtr WidProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled){if (msg 0x112){if (wParam.ToInt32() 0xF093)//单击打开菜单handled true;if (wParam.ToInt32() 0xF100) //键盘打开菜单 handled true;}if (msg 0xa4){if (wParam.ToIn…

Vert.x的TCP服务端和客户端配置

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通信&a…

【python】【爬虫】爬取Fate Grand Order wiki所有英灵礼装图鉴

import requests from lxml import etreefor i in range(1,895): url0"https://fgowiki.com/guide/equipdetail/894?ppc" #网页地址resrequests.get(url0)contentres.contenthtmletree.HTML(content)namehtml.xpath(//*[id"row-move"]/div[2]/div/div[2…

TCP解读

TCP 三次握手 第一次: 客户端 - - > 服务端 发送目标&#xff1a;我要连你 第二次: 客户端 < - - 服务端 发送目标&#xff1a;可以 第三次: 客户端 - - > 服务端 发送目标&#xff1a;收到 TCP 详解&#xff1a;https://blog.csdn.net/sinat_36629696/article/detail…

Mysql 内置函数大全

转载并整理&#xff1a; 包括&#xff1a; 数值进制&#xff1b; 字符串长度、截取、填充、删除、拼接等&#xff1b; 文件读取&#xff1b; 数值绝对值、正负判断、取模、取整、次方、对数、开方、三角函数、反三角函数、随机数、弧度和角度、精确度保留、最大值和最小值…

【Hadoop】自定义Hadoop序列化been Demo

package hadoop.mapreduce.serializable;import org.apache.hadoop.io.Writable;import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; /* * 自定义Hadoop序列化 * */ public class MySerializable implements Writable {private String name;pr…

Mysql加解密

SELECT PASSWORD(you); # *1A11AE440F0BFE14CF065EA776CEFA20B3BCF946 SELECT MD5(you); # 639bae9ac6b3e1a84cebb7b403297b79 /* you为明文&#xff0c;key为密钥&#xff0c;ENCODE(you, key)为you经key加密后得到的密文 */ SELECT ENCODE(you, key); # l0 /* 对密文l0使用…