The content of this article is about the implementation code of Java port monitoring. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
import java.io.*; import java.net.ServerSocket; import java.net.Socket; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; import java.text.SimpleDateFormat; import java.util.Date; public class test { public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException { // 客户端 SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); try { PrintStream out = new PrintStream("D://Systemout" + format.format(new Date()) + ".txt"); System.setOut(out); } catch (FileNotFoundException e) { e.printStackTrace(); } // System.out.println("aaa"); // 1、创建客户端Socket,指定服务器地址和端口 ServerSocket serverSocket = new ServerSocket(7080); while(true){ Socket socket = serverSocket.accept(); // 2、获取输出流,向服务器端发送信息 /* * Scanner scanner =new Scanner(socket.getInputStream());//创建一个客户端的输入流 * (用于在服务端显示) * * String msg; while(scanner.hasNextLine()){//获取客户端发送的信息 msg=scanner.nextLine(); * System.out.println(msg); } */ BufferedInputStream bis = new BufferedInputStream(socket.getInputStream()); DataInputStream dis = new DataInputStream(bis); byte[] bytes = new byte[1]; // 一次读取一个byte String ret = ""; while (dis.read(bytes) != -1) { ret += bytesToHexString(bytes) + " "; if (dis.available() == 0) { // 一个请求 System.out.print(ret + ' '); } } // String ret = "47 38 30 31 33 35 36 32 fe 0d ff 06 6e ff 53 51 55 65 78 67 19 21 14 c7 cb fe 0c 01 07 e2 09 06 10 01 04 01 2f 01 0a 27 10 00 3c 1c 59 15 1d"; System.out.println("---"); String ret1 = ret.substring(0, ret.length()); System.out.println("有效字符串为:" + ret1); String ret2 = ret1.substring(0, 24); System.out.println("注册码:" + ret2); String ret3 = ret1.substring(24, 75); System.out.println("设备码:" + ret3); String ret4 = ret1.substring(75, ret1.length()); System.out.println("数据:" + ret4); String zcm = ret2.substring(0, ret2.length()).replaceAll(" ", ""); String ss1 = ret4.substring(9, 30).replaceAll(" ", ""); String ss2 = ret4.substring(30, 54).replaceAll(" ", ""); String ss3 = ret4.substring(54, 60).replaceAll(" ", ""); String ss4 = ret4.substring(60, 65).replaceAll(" ", ""); int year = Integer.parseInt(ss1.substring(0, 4).replaceAll("^0[x|X]", ""), 16); System.out.println("年份:" + year); int month = Integer.parseInt(ss1.substring(4, 6).replaceAll("^0[x|X]", ""), 16); System.out.println("月:" + month); int day = Integer.parseInt(ss1.substring(6, 8).replaceAll("^0[x|X]", ""), 16); System.out.println("日:" + day); int hour = Integer.parseInt(ss1.substring(8, 10).replaceAll("^0[x|X]", ""), 16); System.out.println("时:" + hour); int min = Integer.parseInt(ss1.substring(10, 12).replaceAll("^0[x|X]", ""), 16); System.out.println("分:" + min); int sec = Integer.parseInt(ss1.substring(12, 14).replaceAll("^0[x|X]", ""), 16); System.out.println("秒:" + sec); int temp = Integer.parseInt(ss2.substring(0, 4).replaceAll("^0[x|X]", ""), 16); System.out.println("温度:" + temp); int sw = Integer.parseInt(ss2.substring(4, 8).replaceAll("^0[x|X]", ""), 16); System.out.println("水位:" + sw); // 字符串类型的日期与时间直接存储到数据库 String date = String.valueOf(year) + "-" + String.valueOf(month) + "-" + String.valueOf(day); System.out.println(date); String time = String.valueOf(hour) + "时" + String.valueOf(min) + "分" + String.valueOf(sec) + "秒"; System.out.println(time); String driverName = "com.mysql.jdbc.Driver"; String dbURL = "jdbc:mysql://localhost:3306/dire"; String userName = "root"; String userPwd = "sa"; Class.forName(driverName); Connection con = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println("连接数据库成功"); Statement stat = con.createStatement(); String sql = "INSERT INTO shuju(注册码,日期,时间,温度,水位) VALUES(?,?,?,?,?)"; PreparedStatement pstmt; try { pstmt = (PreparedStatement) con.prepareStatement(sql); pstmt.setObject(1, zcm); pstmt.setObject(2, date); pstmt.setObject(3, time); pstmt.setObject(4, temp); pstmt.setObject(5, sw); pstmt.executeUpdate(); pstmt.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); } System.out.println("成功"); } } public static String bytesToHexString(byte[] src) { StringBuilder stringBuilder = new StringBuilder(""); if (src == null || src.length <= 0) { return null; } for (int i = 0; i < src.length; i++) { int v = src[i] & 0xFF; String hv = Integer.toHexString(v); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } return stringBuilder.toString(); } }
Related recommendations:
Dynamic listening/static listening experiment summary of default and non-default ports
How to solve socket When listening to a port, only one client can send data to the port
The above is the detailed content of Java port listening implementation code. For more information, please follow other related articles on the PHP Chinese website!