Implementation code for Java port monitoring

Source: Internet
Author: User
Tags stmt
This article gives you the content is about the Java port Monitoring implementation code, there is a certain reference value, there is a need for friends to refer to, I hope you have some help.

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 {//client 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, create client socket, specify server address and port ServerSocket serversocket = new ServerSocket (7080); while ( true) {Socket socket = serversocket.accept ();//2, get output stream, send message to server/* * Scanner Scanner =new Scanner (socket.getinputstream ());//Create a client input stream * (for display on the server) * * String msg; while (Scanner.hasnextline ()) {//Gets the information sent by the client msg=scanner.nextline (); * SYSTEM.OUT.PRINTLN (msg);} */bufferedinputstream bis = new Bufferedinputstream (Socket.getinputstream ());D atainputstream dis = new DataInputStream (bis); byte[] bytes = new Byte[1];  Read one bytestring ret at a time = "" while (Dis.read (bytes)! =-1) {ret + = bytestohexstring (bytes) + ""; if (dis.available () = = 0) {//A request System.out.print (ret + ');}} String ret = "06" (09) 04 FE 0d FF 6e FF-All-in-a-C7 CB Fe 0c-0 1 2f 0a, 3c 1c, 1d "; SYSTEM.OUT.PRINTLN ("---"); String Ret1 = ret.substring (0, Ret.length ()); System.out.println ("Valid string is:" + ret1); String Ret2 = ret1.substring (0, 24); SYSTEM.OUT.PRINTLN ("Registration code:" + Ret2); String Ret3 = ret1.substring (24, 75); SYSTEM.OUT.PRINTLN ("Device code:" + Ret3); String Ret4 = ret1.substring (, ret1.length ()); SYSTEM.OUT.PRINTLN ("Data:" + ret4); String zcm = ret2.substring (0, Ret2.length ()). ReplaceAll ("", ""); String SS1 = ret4.substring (9, +). ReplaceAll ("", ""); String ss2 = ret4.substring (si, si). ReplaceAll ("", ""); String SS3 = ret4.suBstring (SI), ReplaceAll ("", ""); String SS4 = ret4.substring (.) ReplaceAll ("", ""); int year = Integer.parseint (ss1.substring (0, 4). ReplaceAll ("^0[x | X] "," "), 16); System.out.println ("Years:" + year), int month = Integer.parseint (ss1.substring (4, 6). ReplaceAll ("^0[x| X] "," "), 16); System.out.println ("Month:" + month); int day = Integer.parseint (ss1.substring (6, 8). ReplaceAll ("^0[x| X] "," "), 16); System.out.println ("Days:" + day); int hour = Integer.parseint (ss1.substring (8, ten). ReplaceAll ("^0[x| X] "," "), 16); System.out.println ("Time:" + hour); int min = Integer.parseint (ss1.substring (Ten). ReplaceAll ("^0[x| X] "," "), 16); System.out.println ("min:" + min); int sec = Integer.parseint (ss1.substring). ReplaceAll ("^0[x| X] "," "), 16); System.out.println ("Seconds:" + sec); int temp = Integer.parseint (ss2.substring (0, 4). ReplaceAll ("^0[x| X] "," "), 16); SYSTEM.OUT.PRINTLN ("Temp:" + temp); int sw = Integer.parseint (ss2.substring (4, 8). ReplaceAll ("^0[x| X] "," "), 16); System.out.println ("Water level:" + sw);//The date and time of the string type are stored directly to the numberAccording to the library String date = string.valueof (year) + "-" + string.valueof (Month) + "-" + string.valueof (day); SYSTEM.OUT.PRINTLN (date); String time = string.valueof (hour) + "when" + string.valueof (min) + "minute" + string.valueof (sec) + "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 ("Connection to Database Success"); Statement stat = con.createstatement (); String sql = "INSERT into Shuju (registration code, date, time, temperature, water level) VALUES (?,?,?,?,?)"; PreparedStatement pstmt;try {pstmt = (preparedstatement) con.preparestatement (SQL);p stmt.setobject (1, zcm); Pstmt.setobject (2, date);p Stmt.setobject (3, time);p Stmt.setobject (4, temp);p stmt.setobject (5, SW); Pstmt.executeupdate ();p stmt.close (); Con.close ();} catch (SQLException e) {e.printstacktrace ();} SYSTEM.OUT.PRINTLN ("Success");}} 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 Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.