@Component @Configuration //1.主要用於標記配置類,兼備Component的效果。 @EnableScheduling public class InitJob implements CommandLineRunner { @Override public void run(String... args) throws Exception { boolean started = false; ServerSocket serverSocket = null; //1.建立服務端ServerSocket和客戶端Socket try { serverSocket = new ServerSocket(11008); started = true; System.out.println("端口已開啓,佔用11008端口號...."); } catch (BindException e) { System.out.println("端口使用中...."); System.out.println("請關掉相關程序並重新運行服務器!"); System.exit(0); } catch (IOException e) { e.printStackTrace(); } try { // 輪流等待請求 while (true) { // 等待客戶端請求,無請求則閒置;有請求到來時,返回一個對該請求的socket連接 Socket clientSocket = serverSocket.accept(); System.out.println("客戶端:" + clientSocket.getInetAddress().getHostAddress() + "已連接到服務器"); // 創建zithread對象,通過socket連接通信 Thread t = new Thread(new SocketServer(clientSocket)); t.start(); } } catch (IOException e) { e.printStackTrace(); } } }
public class SocketServer implements Runnable{ private Socket socket; private InputStream in; private DataOutputStream dos = null; private OutputStream os = null; public static WaterValueVo waterValueVo; public static WaterValueVo setWaterValueVo(WaterValueVo waterValue) { if (waterValue == null) { waterValueVo = new WaterValueVo(); return waterValueVo; } waterValueVo = waterValue; return waterValueVo; } public static WaterValueVo getWaterValueVo() { if (waterValueVo == null) { waterValueVo = new WaterValueVo(); return waterValueVo; } return waterValueVo; } //2.打開連接到Socket的輸出輸入流 public SocketServer(Socket clientSocket) { try { // 得到socket連接 socket = clientSocket; // 得到客戶端發來的消息 dos = new DataOutputStream(socket.getOutputStream()); in= socket.getInputStream(); os = socket.getOutputStream(); //向客戶端發送消息 sendHeartbeat(); } catch (IOException e) { e.printStackTrace(); } } //3.按照協議進行讀寫操作 public void run() { try { byte[] buffer = new byte[300]; int len = -1; while ((len = in.read(buffer)) != -1) { //輸出獲取到所有字節 16進制 //01 01 03 14 41 AC 00 00 3E 8F 5C 29 41 AC 00 00 3E 8F 5C 29 3F 8A E1 48 9A 67 String message = bytesToHexString(buffer); String code = message.substring(0,2); waterValueVo.setMessage(code); String wenDu = message.substring(8,16); Float wenDuValue = Float.intBitsToFloat(Integer.valueOf(wenDu.trim(), 16)); waterValueVo.setWenDuValue(Float.toString(wenDuValue)); String shuiWei = message.substring(16,24); Float shuiWeiValue = Float.intBitsToFloat(Integer.valueOf(shuiWei.trim(), 16)); waterValueVo.setShuiWeiValue(Float.toString(shuiWeiValue)); String liuSu = message.substring(24,32); Float liuSuValue = Float.intBitsToFloat(Integer.valueOf(liuSu.trim(), 16)); waterValueVo.setLiuSuValue(Float.toString(liuSuValue)); String shunShi = message.substring(32,40); Float shunShiValue = Float.intBitsToFloat(Integer.valueOf(shunShi.trim(), 16)); waterValueVo.setShunShiValue(Float.toString(shunShiValue)); String jiLei = message.substring(40,48); Float jiLeiValue = Float.intBitsToFloat(Integer.valueOf(jiLei.trim(), 16)); waterValueVo.setJiLeiValue(Float.toString(jiLeiValue)); } }catch (IOException e){ e.printStackTrace(); } } //服務端心跳-----定期發送 public void sendHeartbeat() { try { final String heartbeat = "01 03 00 A0 00 0A C5 EF "; new Thread(new Runnable() { @Override public void run() { while (true) { try { Thread.sleep(10 * 1000);// 10s發送一次心跳 System.out.println("客戶端的心跳"); byte [] asd =hex2byte(heartbeat); os.write(asd); os.flush(); } catch (Exception e) { e.printStackTrace(); } } } }).start(); } catch (Exception e) { e.printStackTrace(); }finally { try { if (os == null) { os.close(); } } catch (IOException e) { e.printStackTrace(); } } } //接收數據類型轉換 public static final String bytesToHexString(byte[] bArray) { StringBuffer sb = new StringBuffer(bArray.length); String sTemp; for (int i = 0; i < bArray.length; i++) { sTemp =Integer.toHexString(0xFF & bArray[i]); if (sTemp.length() < 2) sb.append(0); sb.append(sTemp.toUpperCase()); } return sb.toString(); } //心跳包數據----轉換Hex public static byte[] hex2byte(String hex) { String digital = "0123456789ABCDEF"; String hex1 = hex.replace(" ", ""); char[] hex2char = hex1.toCharArray(); byte[] bytes = new byte[hex1.length() / 2]; byte temp; for (int p = 0; p < bytes.length; p++) { temp = (byte) (digital.indexOf(hex2char[2 * p]) * 16); temp += digital.indexOf(hex2char[2 * p + 1]); bytes[p] = (byte) (temp & 0xff); } return bytes; } //----------------------------------------短鏈接~demo // boolean started = false; // ServerSocket ss = null; // Socket s = null; // String msg = "0"; // public static WaterValueVo waterValueVo; // // public static WaterValueVo setWaterValueVo(WaterValueVo waterValue) { // if (waterValue == null) { // waterValueVo = new WaterValueVo(); // return waterValueVo; // } // // waterValueVo = waterValue; // return waterValueVo; // } // // public static WaterValueVo getWaterValueVo() { // if (waterValueVo == null) { // waterValueVo = new WaterValueVo(); // return waterValueVo; // } // return waterValueVo; // } // // public static void serverLink() { // new SocketServer().start(); // } // // public void start() { // ServerSocket serverSocket = null; // Socket socket = null; // String msg = "01 03 00 a0 00 0a C5 EF "; // try { // try { // serverSocket = new ServerSocket(11008); // started = true; // System.out.println("端口已開啓,佔用11008端口號...."); // } catch (BindException e) { // System.out.println("端口使用中...."); // System.out.println("請關掉相關程序並重新運行服務器!"); // System.exit(0); // } catch (IOException e) { // e.printStackTrace(); // } // socket = serverSocket.accept(); // OutputStream os = socket.getOutputStream(); // byte[] asd = hex2byte(msg); // os.write(asd); // InputStream is = socket.getInputStream(); // Integer sourcePort = socket.getPort(); // byte[] message = new byte[500]; // is.read(message); // String messageOpen = ""; // try { // messageOpen = new String(message); // } catch (Exception e) { // e.printStackTrace(); // } // // System.out.println("服務器(收到來自於端口:" + sourcePort + "的信息:" + new String(message)); // String ip = socket.getInetAddress().getHostAddress(); // int port = serverSocket.getLocalPort(); // WaterValueVo waterValue = new WaterValueVo(); // waterValue.setIp(ip); // waterValue.setPort(port); // waterValue.setMessage(messageOpen); // SocketServer.setWaterValueVo(waterValue); // } catch (IOException e) { // e.printStackTrace(); // } finally { // //操作結束,關閉socket // try { // serverSocket.close(); // socket.close(); // } catch (IOException e) { // e.printStackTrace(); // } // } // // } // // public static byte[] hex2byte(String hex) { // String digital = "0123456789ABCDEF"; // String hex1 = hex.replace(" ", ""); // char[] hex2char = hex1.toCharArray(); // byte[] bytes = new byte[hex1.length() / 2]; // byte temp; // for (int p = 0; p < bytes.length; p++) { // temp = (byte) (digital.indexOf(hex2char[2 * p]) * 16); // temp += digital.indexOf(hex2char[2 * p + 1]); // bytes[p] = (byte) (temp & 0xff); // } // return bytes; // } // // // public static byte[] decodeHex(String nm) { // int len = nm.length(); // byte[] result = new byte[len / 2]; // for (int i = 0; i < len; i++) { // char c = nm.charAt(i); // byte b = Byte.decode("0x" + c); // c = nm.charAt(++i); // result[i / 2] = (byte) (b << 4 | Byte.decode("0x" + c)); // } // return result; // } }
===========================僅供參考,代碼練手,不喜勿噴,請多指教=============================