Java网络Socket编程-TCP客户端

实现了一个TCP客户端的监控线程,用于检测TCP服务器是否可用。主要功能如下:

1. 通过指定的IP地址和端口号连接到TCP服务器,并在连接成功后启动一个读取线程用于接收服务器的响应消息。

2. 在接收到服务器响应时,判断响应消息是否与预期消息相同,如果不相同则认为服务器异常,关闭当前连接并重新创建一个新的连接。

3. 通过心跳机制定时检测服务器是否正常,如果连接超时或者出现异常,则更新客户端状态为关闭状态,并在一定时间后重新尝试连接。

4. 监控线程可以通过调用 stopThread() 方法来停止运行,并且可以通过调用 restartThread() 方法来重新启动一个新的线程。

5. 该代码实现了多线程并发执行,具有较好的性能和可靠性。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class TcpClientThread extends Thread{


    private volatile boolean running = true;



    private Long tcpclientId;
    private String tcpclientIp;
    private Integer tcpclientPort;
    private Integer reconnectDelay; // 重连延迟,单位:毫秒
    private String tcpclientMsg;

    private IMonTcpclientService monTcpclientService;

    private String status = "";

    private volatile Integer reconnectTimes = 0;



    public TcpClientThread(Long tcpclientId, String tcpclientIp, Integer tcpclientPort, Integer reconnectDelay, String tcpclientMsg, IMonTcpclientService monTcpclientService){
        this.tcpclientId = tcpclientId;
        this.tcpclientIp = tcpclientIp;
        this.tcpclientPort = tcpclientPort;
        this.reconnectDelay = reconnectDelay;
        this.tcpclientMsg = tcpclientMsg;
        this.monTcpclientService = monTcpclientService;
    }

    public void stopThread() {
        running = false;
    }
    private void restartThread() {
        running = false;
        TcpClientThread tcpClientThread = new TcpClientThread(tcpclientId, tcpclientIp, tcpclientPort,
                reconnectDelay, tcpclientMsg,monTcpclientService);
        TcpClientMonitor.executorService.execute(tcpClientThread);
        TcpClientMonitor.tcpClientMap.put(tcpclientId,tcpClientThread);

    }


    public void run() {
        while (running){

            Socket socket = null;
            try {
                socket = new Socket(tcpclientIp, tcpclientPort);
//                System.out.println("已连接到服务器");
                BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));


                // 在此处执行与服务器的通信逻辑
                char[] buffer = new char[tcpclientMsg.length()]; // 固定长度为8字节

                Thread readerThread = new Thread(() -> {
                    try {
                        int bytesRead;
                        while ((bytesRead = reader.read(buffer, 0, buffer.length)) != -1) {
                            String hexResponse = new String(buffer, 0, bytesRead);
//                            System.out.println("读取到输入:" + hexResponse);
                            if(StringUtils.isBlank(tcpclientMsg)||"#".equals(tcpclientMsg)){
                                reconnectTimes = 0;
                            }else{
                                if(StringUtils.isNotBlank(hexResponse)&&hexResponse.equals(tcpclientMsg)){
                                    reconnectTimes = 0;
                                }
                            }
                        }
                        //socket异常关闭  停止线程->启动新线程
                        restartThread();

                    } catch (Exception e) {
//                        System.err.println("读取输入时发生异常:" + e.getMessage());
                    }
                });

                readerThread.start(); // 启动读取线程

                while (running){
                    try {
                        Thread.sleep(1000);
                        reconnectTimes++;
//                        System.out.println("reconnectTimes*1000:"+reconnectTimes*1000);
                        if(reconnectTimes*1000>reconnectDelay){//收到消息的心跳间隔大于设置的时间
//                            System.out.println("close");
                            if(!"close".equals(status)) {
//                                System.out.println("close-update base");
                                monTcpclientService.updateMonTcpclientStatus(tcpclientId, Status.CLOSE);
                                status = "close";
                            }
                            reconnectTimes --;
                        }else{
//                            System.out.println("open");
                            if(!"open".equals(status)) {
//                                System.out.println("open-update base");
                                monTcpclientService.updateMonTcpclientStatus(tcpclientId, Status.OPEN);
                                status = "open";
                            }
                        }
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
//                System.out.println("停止解析");
                readerThread.interrupt();
            } catch (IOException e) {
//                System.err.println("无法连接到服务器:" + e.getMessage());
                if(!"close".equals(status)) {
                    monTcpclientService.updateMonTcpclientStatus(tcpclientId, Status.CLOSE);
                    status = "close";
                }


            }finally {
                try {
                    if (socket != null) {
                        socket.close();
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }finally {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
//        System.out.println("停止线程");
    }




}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值