package com.code.modules.roaddiseasecontroller; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.code.modules.utils.SpringContextUtil; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.net.Socket; import java.util.HashMap; import java.util.Map; /* * 多线程通信 */ public class TcpThread extends Thread{//implements Runnable Socket socket; public TcpThread(Socket s){ socket=s; } /* * 信息追加 */ public String echo(String str){ return "hello:"+str; } @Override public void run() { String charsetName = "gbk"; try { System.out.println("完成连接"); // 接受客户端信息 boolean xintiao=false; do { InputStream in = socket.getInputStream(); byte[] data = new byte[1024]; int len = in.read(data); String str = new String(data, 0, len, "gbk"); String outstr = null; boolean flag = true; Map map = new HashMap(); try { map = (Map) JSON.parse(str); } catch (Exception e) { flag = false; Map checkData = new HashMap(); checkData.put("result", "1"); checkData.put("msg", "json格式错误"); outstr = JSON.toJSONString(RoadDiseaseController.resultMap(checkData)); System.out.println(e.getMessage()); } if (flag) { RoadDiseaseController controller = (RoadDiseaseController) SpringContextUtil.getBean(RoadDiseaseController.class); Map iddr = (Map) map.get("iddr"); //进入心跳 if (iddr.get("postion") != null) { Map postion = (Map) iddr.get("postion"); JSONArray list = (JSONArray) postion.get("list"); if(list!=null && list.size()>0){ //批量插入心跳数据 outstr = controller.GPSBatchList(list); xintiao=false; }else { outstr = controller.GPS(postion); xintiao=true; } }else{ xintiao=false; } //道路病害数据 if (iddr.get("info") != null) { Map info = (Map) iddr.get("info"); outstr = controller.roadDiseaseData(info); } //新增的接口 if (iddr.get("action") != null) { Map action = (Map) iddr.get("action"); String method = action.get("method") + ""; //设备运行日志 if ("DEVICELOG".equals(method)) { outstr = controller.crackDeviceOnlineData(action); } else if ("CRACKRESULT".equals(method)) { // CRACKRESULT 病害数据状态 outstr = controller.crackresult(action); } else if ("CRACKRESULTCOMFIRM".equals(method)) { // CRACKRESULTCOMFIRM 病害数据状态 outstr = controller.crackresultcomfirm(action); } else if ("GEOCODER".equals(method)) { // GEOCODER 地理编码道路 outstr = controller.geocoder(action); } System.out.println("-------------返回的数据结果------------"); System.out.println(outstr); } } if(RoadDiseaseController.isNull(outstr)){ Map checkData = new HashMap(); checkData.put("result", "1"); checkData.put("msg", "数据处理失败"); outstr = JSON.toJSONString(RoadDiseaseController.resultMap(checkData)); } System.out.println("-------------开始写导入socket客户端------------"); System.out.println(outstr); OutputStreamWriter out = new OutputStreamWriter(socket.getOutputStream(), charsetName); out.write(outstr); out.flush(); System.out.println("-------------开始写导入socket客户端 结束------------"); }while (xintiao); if(!xintiao){ socket.close(); } } catch (Exception e) { System.out.println(e.getMessage()); ; // 输出异常信息 }finally { try { socket.close(); } catch (IOException e) { System.out.println(e.getMessage()); } } } }
package com.code.modules.roaddiseasecontroller; import org.springframework.stereotype.Component; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; @Component public class TCPServerController { private PrintWriter writer; private ServerSocket server; // 创建ServerSocket对象 private Socket socket; // 创建Socket对象socket public void serve() { try { server = new ServerSocket(8009); // 实例化ServerSocket对象,设置端口为8080 System.out.println("服务器套接字已经创建成功"); } catch (Exception e) { System.out.println("================="); System.out.println("创建服务失败"); e.printStackTrace(); // 输出异常信息 } while (true) { // 如果套接字是连接状态 try { System.out.println("等待客户机的连接......."); socket = server.accept(); // 实例化Socket对象,使用accept方法等待客户机连接 System.out.println("完成连接"); TcpThread t = new TcpThread(socket); System.out.println(t.getName()); t.start(); } catch (Exception e) { System.out.println(e.getMessage()); ; // 输出异常信息 } } } public static void main(String[] args) { // 主方法 TCPServerController tcp = new TCPServerController(); // 创建本类对象 tcp.serve(); // 调用方法 } }