Quellcode durchsuchen

- Socket服务端创建;

Gizmo vor 3 Jahren
Ursprung
Commit
b67b516a31

+ 43 - 0
src/main/java/com/warewms/common/utils/SocketUtils.java

@@ -0,0 +1,43 @@
+package com.warewms.common.utils;
+
+/**
+ * PackageName:com.demo.constant
+ * Description:
+ * Author:Gizmo
+ * Date:2021/2/27
+ */
+public class SocketUtils {
+
+    /**
+     * 计算校验位 ,返回十六进制校验位
+     * @param data 16进制字符串
+     * @return checkSum
+     */
+    private static String makeCheckSum(String data) {
+        int dSum = 0;
+        int length = data.length();
+        int index = 0;
+        // 遍历十六进制,并计算总和
+        while (index < length) {
+            String s = data.substring(index, index + 2); // 截取2位字符
+            dSum += Integer.parseInt(s, 16); // 十六进制转成十进制 , 并计算十进制的总和
+            index = index + 2;
+        }
+
+        int mod = dSum % 256; // 用256取余,十六进制最大是FF,FF的十进制是255
+        String checkSumHex = Integer.toHexString(mod).toUpperCase(); // 余数转成十六进制
+        length = checkSumHex.length();
+        if (length < 2) {
+            checkSumHex = "0" + checkSumHex;  // 校验位不足两位的,在前面补0
+        }
+        return checkSumHex;
+    }
+
+    public static void main(String[] args) {
+
+        String data = "01 00 00 00 00 01 0A 08 7A 68 61 6E 67 73 61 6E 10 14 1A 04 6D 61 6C 65";
+        data = data.replace(" ", "");
+        String result = makeCheckSum(data);
+        System.out.println(result);
+    }
+}

+ 25 - 0
src/main/java/com/warewms/init/InitServlet.java

@@ -0,0 +1,25 @@
+package com.warewms.init;
+
+import com.warewms.socket.SocketServerListenHandler;
+import org.springframework.boot.CommandLineRunner;
+import org.springframework.core.annotation.Order;
+import org.springframework.stereotype.Component;
+
+/**
+ * PackageName:com.warewms.init
+ * Description:AutoRunner
+ * Author:Gizmo
+ * Date:2021/2/27
+ */
+@Component
+@Order(0)
+public class InitServlet implements CommandLineRunner {
+
+    @Override
+    public void run(String... args) throws Exception {
+
+        //作为观光车通讯的服务端
+        SocketServerListenHandler listenHandler = new SocketServerListenHandler(9700);
+        listenHandler.listenClientConnect();
+    }
+}

+ 55 - 0
src/main/java/com/warewms/socket/SocketServerClientHandler.java

@@ -0,0 +1,55 @@
+package com.warewms.socket;
+
+import com.fdd.protobuf.InfoProto;
+import com.google.protobuf.ByteString;
+
+import java.io.DataInputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.Socket;
+
+/**
+ * 服务端客户消息处理线程类
+ */
+public class SocketServerClientHandler extends Thread {
+
+    //每个消息通过Socket进行传输
+    private Socket clientConnectSocket;
+
+    public SocketServerClientHandler(Socket clientConnectSocket) {
+        this.clientConnectSocket = clientConnectSocket;
+    }
+
+    @Override
+    public void run() {
+        try {
+            InputStream is = clientConnectSocket.getInputStream();
+            OutputStream os = clientConnectSocket.getOutputStream();
+            while (true) {
+                DataInputStream dis = new DataInputStream(is);
+                //读取长度
+                int lenData = dis.readInt();
+
+                InfoProto.carinfo carInfo = InfoProto.carinfo.parseFrom(ByteString.readFrom(is));
+                if (null != carInfo) {
+                    System.out.println("server received data:\n" + carInfo.toString());
+
+                    carInfo = InfoProto.carinfo.newBuilder().setTimestampUs(1000).setVid(999).setUsr("Gizmo").setPsw("123456").setCarId("V01").build();
+                    os.write(carInfo.toByteArray());
+                    os.flush();
+                }
+
+//                while ((len = is.read(data)) != -1) {
+//
+//                    String message = new String(data, 0, len);
+//                    System.out.println("客户端传来消息: " + message);
+//                    os.write(data);
+////                    os.flush();
+//                }
+            }
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 42 - 0
src/main/java/com/warewms/socket/SocketServerListenHandler.java

@@ -0,0 +1,42 @@
+package com.warewms.socket;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.net.ServerSocket;
+import java.net.Socket;
+
+/**
+ * 服务端监听类
+ */
+@Slf4j
+public class SocketServerListenHandler {
+
+    //这是一个不断等待获取网络传入请求的服务端Socket
+    private ServerSocket serverSocket;
+
+    /**
+     * 构造方法
+     * @param port 端口
+     */
+    public SocketServerListenHandler(int port) {
+        try {
+            serverSocket = new ServerSocket(port);
+        } catch (Exception e) {
+            log.error("Socket构造错误,errorMsg:" + e.toString());
+        }
+    }
+
+    public void listenClientConnect() {
+
+        while (true) {
+            try {
+                log.info("服务端监听开始...");
+                Socket clientConnectSocket = serverSocket.accept();
+                log.info("监听到客户端连接。。。创建处理客户端连接的handler工具。。");
+                new SocketServerClientHandler(clientConnectSocket).start();
+            } catch (Exception e) {
+                log.error("服务端监听连接程序异常,errorMsg:" + e.toString());
+            }
+        }
+    }
+}