本文主要是介绍Socket工具类开箱即用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
不多说,Socket工具类开箱即用:
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;/*** Socket工具类*/
@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);}// 构造器2public 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;}
}
这篇关于Socket工具类开箱即用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!