自动化立体仓库 - WCS系统
#
lsh
2022-09-28 7b3e75fed1d44d89bd7a06c0e5ed98cddf039c31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.zy.core.thread;
 
import com.zy.core.ThreadHandler;
import com.zy.core.model.SocketSlave;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
 
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
 
/**
 * 消防socket线程
 * Created on 2022/7/30
 */
@Data
@Slf4j
public class SocketThread implements Runnable, ThreadHandler {
 
    private SocketSlave slave;
//    private String barcode;
//    private Socket socket;
//    private DataOutputStream dataOutputStream;
//    private DataInputStream dataInputStream;
 
    private byte[] byteData = new byte[1024];
 
    public SocketThread(SocketSlave slave) {
        this.slave = slave;
    }
 
    @Override
    @SuppressWarnings("InfiniteLoopStatement")
    public void run() {
//        this.connect();
        while (true) {
            try {
//                byte[] read = read(8, 200);
//                if (null != read) {
//                    String s = new String(read);
////                    if (!Cools.isEmpty(s)) {
////                        barcode = new String(read);
////                        log.info("{}号条码器,检索数据:{}", slave.getId(), this.barcode);
////                        JSONObject jsonObject = new JSONObject();
////                        jsonObject.put("time", DateUtils.convert(new Date(), DateUtils.yyyyMMddHHmmss_F));
////                        jsonObject.put("barcode", barcode);
////                        if (OutputQueue.BARCODE.size() >= 32) {
////                            OutputQueue.BARCODE.poll();
////                        }
////                        OutputQueue.BARCODE.offer(jsonObject);
////                    }
//                }
//                Thread.sleep(50);
//            } catch (SocketTimeoutException ignore) {
            } catch (Exception e) {
//                e.printStackTrace();
            }
        }
    }
 
    public byte[] getByteData() {
        return byteData;
    }
 
    public void setByteData(byte[] byteData) {
        this.byteData = byteData;
    }
 
    @Override
    public boolean connect() {
        return false;
    }
 
    @Override
    public void close() {
//        try {
//            if (null != dataOutputStream) {
//                dataOutputStream.close();
//            }
//            if (null != dataInputStream) {
//                dataInputStream.close();
//            }
////        if (null != socket && !socket.isClosed()) {
////            socket.close();
////        }
//            if (null != socket){
//                socket.close();
//            }
//            socket = null;
//        } catch (IOException e) {
//            log.error("SocketClient close Exception:" + e.getMessage());
//        }
    }
 
//    public void write(byte[] msg, int len) throws IOException {
//        if (null != dataInputStream)
//        {
//            dataOutputStream.write(msg, 0, len);
//            dataOutputStream.flush();
//        }
//    }
//
//    public byte[] read(int bufferSize, int timeOut) throws IOException {
//        if (socket == null || !socket.isConnected() || socket.isClosed()) {
//            connect();
//        }
////        connect();
//        socket.setSoTimeout(timeOut);
//        byte[] bytes = new byte[bufferSize];
//        int len = dataInputStream.read(bytes);
//        byte[] tempBytes = null;
//        if (len > 0) {
//            tempBytes = new byte[len];
//            System.arraycopy(bytes, 0, tempBytes, 0, len);
//        } else {
//            connect();
//        }
//        return tempBytes;
//    }
 
//    public boolean valid() throws Exception {
//        if (null == socket || socket.isClosed() || socket.isInputShutdown() || socket.isOutputShutdown()) {
//            if (dataInputStream != null) {
//                dataInputStream.close();
//            }
//            if (dataOutputStream != null) {
//                dataOutputStream.close();
//            }
//            if (socket != null) {
//                socket.close();
//            }
//            return false;
//        }
//        return true;
//    }
 
    /******************************************************************************************/
    /**************************************** 测试专用 *****************************************/
    /*****************************************************************************************/
    public static void main(String[] args) throws InterruptedException {
        try{
            ServerSocket socket = new ServerSocket(8802);
            System.out.println("套接字创建成功,等待连接...");
 
            while (true){
                final Socket con = socket.accept();
                InetAddress addr = con.getInetAddress();
                System.out.println("客户端接入===>>" + addr.getHostAddress() + ":" + con.getPort());
 
                new Thread((new Runnable() {
                    @Override
                    public void run() {
                        InputStream in;
 
                        while (true){
                            try{
                                in = con.getInputStream();
                                int len = 0;
                                byte[] data = new byte[128];
                                len = in.read(data);
                                if(len>0) {
                                    System.out.println("读到消息===>>" + new String(data, 0, len));
                                } else {
//                                    System.out.println("客户端断开===>>");
                                }
                            }catch (IOException e){
                                e.printStackTrace();
                            }
                        }
                    }
                })).start();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
 
}