Java連接埠監聽的實現代碼

來源:互聯網
上載者:User
本篇文章給大家帶來的內容是關於Java連接埠監聽的實現代碼,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所協助。

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]; // 一次讀取一個byteString 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();}}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.