ZengTao
2024-09-03 3744ee1fd763d7414a38f515ba13224866b924aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.mes.tools;
 
import lombok.extern.slf4j.Slf4j;
 
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
 
/**
 * @Author : zhoush
 * @Date: 2024/9/2 15:32
 * @Description:
 */
 
@Slf4j
public class SocketUtil {
    private final String host;
    private final int port;
 
    private final int connectionTimeout;
    private final int soTimeout;
 
    private Socket socket;
 
    private DataOutputStream dataOutputStream;
 
    private DataInputStream dataInputStream;
 
    public SocketUtil(String host, int port) {
        this(host, port, 5000, 5000);
    }
 
    public SocketUtil(String host, int port, int connectionTimeout, int soTimeout) {
        this.host = host;
        this.port = port;
        this.connectionTimeout = connectionTimeout;
        this.soTimeout = soTimeout;
    }
 
    /**
     * 打开Socket连接
     *
     * @return 是否成功打开Socket连接
     */
    public boolean openSocket() {
        try {
            socket = new Socket();
            InetSocketAddress socketAddress = new InetSocketAddress(host, port);
            socket.setReuseAddress(true);
            socket.setSoTimeout(soTimeout);
            socket.connect(socketAddress, connectionTimeout);
            dataOutputStream = new DataOutputStream(socket.getOutputStream());
            dataInputStream = new DataInputStream(socket.getInputStream());
        } catch (Exception e) {
            log.error("打开Socket连接失败:", e);
            return false;
        }
        return true;
    }
 
    public boolean sendData(String data) {
        return sendData(data, "utf-8");
    }
 
    /**
     * 发送请求数据。
     *
     * @param data    数据
     * @param charset 字符集
     * @return 是否发送成功
     */
    public boolean sendData(String data, String charset) {
        boolean isSend = true;
        try {
            byte[] bytes = data.getBytes(charset);
            dataOutputStream.writeInt(bytes.length);
            dataOutputStream.write(bytes);
        } catch (IOException e) {
            log.error("发送数据失败:", e);
            isSend = false;
        }
        return isSend;
    }
 
    /**
     * 接收响应数据
     */
    public String receiveData() {
        return receiveData("UTF-8");
    }
 
    /**
     * 接收响应数据
     */
    public String receiveData(String charset) {
        String returnStr = null;
        byte[] data;
        try {
            int length = dataInputStream.readInt();
            if (length > 0) {
                data = new byte[length];
                dataInputStream.readFully(data);
                returnStr = new String(data, charset);
            }
        } catch (IOException e) {
            log.error("接收数据失败:", e);
        }
        return returnStr;
    }
 
    /**
     * 关闭Socket连接和输入输出流
     */
    public void close() {
        try {
            if (dataInputStream != null) {
                dataInputStream.close();
            }
        } catch (IOException e) {
            log.error("关闭输入流失败:", e);
        }
        try {
            if (dataOutputStream != null) {
                dataOutputStream.close();
            }
        } catch (IOException e) {
            log.error("关闭输出流失败:", e);
        }
        try {
            if (socket != null) {
                socket.close();
            }
        } catch (IOException e) {
            log.error("关闭Socket失败:", e);
        }
    }
 
    /**
     * Socket短连接发送请求数据
     *
     * @param host        host
     * @param port        端口
     * @param requestData 请求数据
     * @return 响应数据
     */
    public static String send(String host, int port, String requestData) {
        SocketUtil socketUtil = new SocketUtil(host, port);
        String resp = null;
        try {
            if (socketUtil.openSocket()) {
                if (socketUtil.sendData(requestData)) {
                    resp = socketUtil.receiveData();
                } else {
                    log.error("SocketUtil.send发送数据失败");
                }
            } else {
                log.error("SocketUtil.openSocket打开Socket失败");
            }
        } catch (Exception e) {
            log.error("SocketUtil.send发送请求失败:", e);
        } finally {
            socketUtil.close();
        }
        return resp;
    }
}