|
@@ -0,0 +1,89 @@
|
|
|
+package com.ruoyi.system;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import org.springframework.boot.CommandLineRunner;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.PrintWriter;
|
|
|
+import java.net.ServerSocket;
|
|
|
+import java.net.Socket;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+@Component
|
|
|
+public class TcpClientHandler extends Thread implements CommandLineRunner {
|
|
|
+
|
|
|
+ private Socket clientSocket;
|
|
|
+ private ServerSocket serverSocket;
|
|
|
+ private PrintWriter out;
|
|
|
+ private InputStream in;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run(String... args) throws Exception
|
|
|
+ {
|
|
|
+
|
|
|
+ try{
|
|
|
+ ServerSocket server = new ServerSocket(8802);
|
|
|
+ Socket client = null;
|
|
|
+ boolean flag = true;
|
|
|
+ while(flag){
|
|
|
+ System.out.println("start...");
|
|
|
+ client = server.accept();
|
|
|
+ new Thread(new EchoThread(client)).start();
|
|
|
+ }
|
|
|
+ client.close();
|
|
|
+ server.close();
|
|
|
+ }catch(Exception ex){
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+// try {
|
|
|
+// serverSocket = new ServerSocket(8802);
|
|
|
+// clientSocket = serverSocket.accept();
|
|
|
+// SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD hh:mm:ss");
|
|
|
+// out = new PrintWriter(clientSocket.getOutputStream(),true);
|
|
|
+// in = clientSocket.getInputStream();
|
|
|
+// while(true)
|
|
|
+// {
|
|
|
+// String inputLine="";
|
|
|
+// byte[] temp = new byte[1024];
|
|
|
+// int length = in.read(temp);
|
|
|
+// inputLine+=new String(temp,0,length);
|
|
|
+// JSONObject object = JSONObject.parseObject(inputLine);
|
|
|
+// String st = object.get("ST").toString();
|
|
|
+// String serial = object.get("SERIAL").toString();
|
|
|
+// if(".".equals(inputLine))
|
|
|
+// {
|
|
|
+// out.println("bye");
|
|
|
+// break;
|
|
|
+// }
|
|
|
+// //传感器上报的数据===>inputLine
|
|
|
+// System.out.println("===="+inputLine);
|
|
|
+// //do sth
|
|
|
+//
|
|
|
+// //do sth
|
|
|
+// JSONObject res = new JSONObject();
|
|
|
+// res.put("ST",st);
|
|
|
+// res.put("SERIAL",serial);
|
|
|
+// res.put("MARK","1");
|
|
|
+// res.put("TM ", sdf.format(new Date()));
|
|
|
+// out.println(res);
|
|
|
+// }
|
|
|
+// in.close();
|
|
|
+// out.close();
|
|
|
+// clientSocket.close();
|
|
|
+// } catch (IOException e) {
|
|
|
+// // TODO Auto-generated catch block
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|