|
@@ -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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|