Java实现socket 客户端 长连接

Java实现socket客户端长连接

import com.teruisa.jinji100.utils.HexUtil;
import com.teruisa.jinji100.utils.SocketUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * socket 长连接:连接建立后不会主动断开,可以持续收发消息
 */
public class ClientThread {

    private final Logger logger = LoggerFactory.getLogger(ClientThread.class);
    private String hosts;
    private int port;
    private Socket socket = null;
    private OutputStream os = null;
    private InputStream is = null;

    public ClientThread(String hosts, int port) {
        this.hosts = hosts;
        this.port = port;
        try {
            this.socket = new Socket(hosts, port);
            this.os = socket.getOutputStream();
            this.is = socket.getInputStream();
            SocketThread socketThread = new SocketThread();
            socketThread.start();
            logger.info("创建socket连接成功,address:{}:{}", hosts, port);
        } catch (UnknownHostException e) {
            logger.error("创建socket连接UnknownHost异常", e);
        } catch (IOException e) {
            logger.error("创建socket连接IO异常", e);
        }
    }

    /**
     * 办公灯光
     * @param msg
     * @return
     */
    public String sendLampComd(String msg) {
        String receiveMsg = "";
        PrintWriter pw = null;
        BufferedReader br = null;
        try {
            pw = new PrintWriter(os);
            pw.write(msg);
            pw.flush();
            socket.shutdownOutput();
            // 从服务器接收的信息
            br = new BufferedReader(new InputStreamReader(is));
            receive(br);
        } catch (IOException e) {
            logger.error("IOException", e);
        } finally {
            // 关闭资源
            SocketUtil.close(br, null, is, os, socket);
        }
        return receiveMsg;
    }

    /**
     * 会议室灯光
     * @param msg
     * @return
     */
    public String sendMeetingComd(String msg) {
        byte[] data = HexUtil.hexStrToBinaryStr(msg);
        return sendCurtainComd(data);
    }

    /**
     * 窗帘
     * @param data
     * @return
     */
    public String sendCurtainComd(byte[] data) {
        String receiveMsg = "";
        BufferedReader br = null;
        try {
            os.write(data);
            socket.shutdownOutput();
            br = new BufferedReader(new InputStreamReader(is));
            receive(br);
        } catch (IOException e) {
            logger.error("IOException", e);
        } finally {
            // 关闭资源
            SocketUtil.close(br, null, is, os, socket);
        }
        return receiveMsg;
    }

    private String receive(BufferedReader br) throws IOException {
        // 从服务器接收的信息
        String receiveMsg = "";
        String reply;
        while((reply = br.readLine()) != null){
            logger.info("设备中控返回信息:{}", reply);
            receiveMsg = reply;
        }
        return receiveMsg;
    }

    /**
     * 发送心跳包
     */
    /*public void sendHeartbeat() {
        try {
            String heartbeat = "ping\r\n";
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        try {
                            Thread.sleep(10 * 1000);// 10s发送一次心跳
                            os.write(heartbeat.getBytes());
                            os.flush();
                        } catch (Exception e) {
                            logger.error("发送心跳异常", e);
                        }
                    }
                }
            }).start();
        } catch (Exception e) {
            logger.error("发送心跳异常", e);
        }
    }*/

    private class SocketThread extends Thread {
        @Override
        public void run() {
            long startTime = System.currentTimeMillis();
            //sendHeartbeat();
            while (true) {
                try {
                    if (socket == null || socket.isClosed()) {
                        socket = new Socket(hosts, port); // 连接socket
                        os = socket.getOutputStream();
                    }
                    Thread.sleep(100);
                    is = socket.getInputStream();
                    int size = is.available();
                    if (size <= 0) {
                        if ((System.currentTimeMillis() - startTime) > 3 * 10 * 1000) { // 如果超过30秒没有收到服务器发回来的信息,说明socket连接可能已经被远程服务器关闭
//                            socket.close(); // 这时候可以关闭socket连接
//                            startTime = System.currentTimeMillis();
                        }
                        continue;
                    } else {
                        startTime = System.currentTimeMillis();
                    }
                    byte[] resp = new byte[size];
                    is.read(resp);
                    String response = new String(resp, "utf-8");
                    logger.info("连接成功:{}:{},server端返回结果:{}", hosts, port, response);
                } catch (Exception e) {
                    e.printStackTrace();
                    try {
                        socket.close();
                        is.close();
                        os.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
    }
}
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值