package com.iot.modbus_rtcp; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; import java.time.LocalDateTime; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.LockSupport; /** * @author 王仕龙 * 2024/11/20 16:01 */ public class NonBlockingSocketTest { private static final String HOST = "127.0.0.1"; private static final Integer PORT = 1200; public static void main(String[] args) throws IOException { SocketChannel socketChannel = SocketChannel.open(); // 设置为非阻塞模式 socketChannel.configureBlocking(false); socketChannel.connect(new InetSocketAddress(HOST, PORT)); while (!socketChannel.finishConnect()) { // 等待连接建立 System.out.println("正在建立连接..."); } long lastSentHeartBeatTime = System.currentTimeMillis(); // 连接已建立,发送和接收数据 // ByteBuffer heartBeatBuffer = ByteBuffer.wrap("Hello, Server!".getBytes()); ByteBuffer heartBeatBuffer = ByteBuffer.wrap("KENENG1400000358".getBytes()); // ByteBuffer heartBeatBuffer = ByteBuffer.wrap("01 04 2A 07 E8 00 0B 00 13 00 17 00 11 00 23 00 03 00 01 00 00 00 00 00 02 00 3B 03 E7 00 39 00 00 0D AC 0D AC 00 00 00 00 00 00 0A 14 4A 98".replaceAll(" ", "").getBytes()); socketChannel.write(heartBeatBuffer); long nowTime = 0L; ByteBuffer readBuffer = ByteBuffer.allocate(1024); while (socketChannel.isConnected()) { readBuffer.clear(); socketChannel.read(readBuffer); readBuffer.flip(); // 是否有可用数据 if (!readBuffer.hasRemaining()) { nowTime = System.currentTimeMillis(); if (nowTime - lastSentHeartBeatTime > 5000) { lastSentHeartBeatTime = nowTime; socketChannel.write(heartBeatBuffer); } LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(500)); continue; } String line = byteBufferToHexString(readBuffer).trim(); lastSentHeartBeatTime = System.currentTimeMillis(); System.out.println(LocalDateTime.now() + "<==:收到服务器端请求:" + line); LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(1)); // 接收到请求 switch (line.toUpperCase()) { case "01040000001531C5" -> // 发送响应字符串 socketChannel.write(ByteBuffer.wrap("01 04 2A 07 E8 00 0B 00 13 00 17 00 11 00 23 00 03 00 01 00 00 00 00 00 02 00 3B 03 E7 00 39 00 00 0D AC 0D AC 00 00 00 00 00 00 0A 14 4A 98".replaceAll(" ", "").getBytes())); // 接收到开井请求 case "01050001FF00DDFA" -> // 发送响应字符串 socketChannel.write(ByteBuffer.wrap("01050001FF00DDFA".getBytes())); // 接收到关井请求 case "0105000100009C0A" -> // 发送响应字符串 socketChannel.write(ByteBuffer.wrap("0105000100009C0A".getBytes())); // 接收到读取运行模式请求 case "010300640001C5D5" -> // 发送响应字符串 socketChannel.write(ByteBuffer.wrap("01 03 02 00 03 F8 45".replaceAll(" ", "").getBytes())); // 接收到退出请求 case "exit" -> socketChannel.close(); } System.out.println(LocalDateTime.now() + "==>:已响应服务器端请求:" + line); } } public static String bytesToHexString(byte[] src) { String strHex = ""; StringBuilder sb = new StringBuilder(50); for (int n = 0; n < src.length; n++) { strHex = Integer.toHexString(src[n] & 0xFF); // 每个字节由两个字符表示,位数不够,高位补0 sb.append((strHex.length() == 1) ? "0" + strHex : strHex); } return sb.toString().trim().toUpperCase(); } public static String byteBufferToHexString(ByteBuffer buffer) { byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); StringBuilder hexString = new StringBuilder(); for (byte b : bytes) { String hex = Integer.toHexString(0xff & b); if (hex.length() == 1) { hexString.append('0'); } hexString.append(hex); } return hexString.toString(); } }