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;
|
}
|
}
|