You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.8 KiB
Java

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

//package com.hw.mes;
//
//import org.springframework.stereotype.Component;
//
//import javax.annotation.PostConstruct;
//import javax.annotation.PreDestroy;
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.OutputStream;
//import java.net.Socket;
//
///**
// * @Description: Scoket客户端长连接
// * @ClassName: SocketClient
// * @Author : xins
// * @Date :2024-04-12 15:03
// * @Version :1.0
// */
//@Component
//public class SocketClient {
//
// private static final String SERVER_HOST = "10.11.41.121";
// private static final int SERVER_PORT = 5000;
// private Socket socket;
// private InputStream inFromServer;
// private OutputStream outToServer;
//
// @PostConstruct
// public void init() {
// try {
// System.out.println("ddddddssss");
// socket = new Socket(SERVER_HOST, SERVER_PORT);
// socket.setSoTimeout(5000); // 设置读取超时例如5秒
// socket.setKeepAlive(true); // 开启TCP KeepAlive有助于检测连接状态
//
// inFromServer = socket.getInputStream();
// outToServer = socket.getOutputStream();
// } catch (IOException e) {
// System.err.println("Error initializing socket client: " + e.getMessage());
// }
// }
//
// public String callApi(String request) {
// try {
// // 发送请求信息
// outToServer.write(request.getBytes());
// outToServer.flush();
//
// // 接收反馈信息
// byte[] buffer = new byte[1024];
// int bytesRead = inFromServer.read(buffer);
// if (bytesRead == -1) {
// throw new IOException("Connection closed while waiting for feedback");
// }
//
// String feedback = new String(buffer, 0, bytesRead);
// System.out.println("Received feedback from server: " + feedback);
//
// // 处理接收到的反馈信息
// handleServerFeedback(feedback);
//
// return feedback; // 返回接收到的反馈信息
// } catch (IOException e) {
// System.err.println("Error calling API: " + e.getMessage());
// // 根据实际情况处理异常,如重试、通知用户等
// return null; // 或返回一个默认值、错误信息等
// }
// }
//
// private void handleServerFeedback(String feedback) {
// System.out.println("Handling server feedback: " + feedback);
// }
//
// //重连
// @PreDestroy
// public void cleanup() {
// try {
// if (socket != null && !socket.isClosed()) {
// socket.close();
// }
// } catch (IOException e) {
// System.err.println("Error closing socket: " + e.getMessage());
// }
// }
//}