package com.zy.core.thread; import com.core.common.Cools; import com.zy.core.Slave; import com.zy.core.ThreadHandler; import lombok.Data; import lombok.extern.slf4j.Slf4j; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; import java.net.SocketTimeoutException; /** * 条码扫描仪线程 * Created by vincent on 2020/8/4 */ @Data @Slf4j public class BarcodeThread implements Runnable, ThreadHandler { private Slave slave; private String barcode; private Socket socket; private DataOutputStream dataOutputStream; private DataInputStream dataInputStream; public BarcodeThread(Slave slave) { this.slave = slave; } @Override @SuppressWarnings("InfiniteLoopStatement") public void run() { connect(); while (true) { try { // write("T".getBytes(), "T".length()); byte[] read = read(11, 200); if (null != read) { barcode = new String(read); } Thread.sleep(100); } catch (SocketTimeoutException ignore) { } catch (Exception e) { // e.printStackTrace(); } } } @Override public boolean connect() { try { close(); //1.主动释放连接 //2.某些服务器对指定ip有链路数限制 socket = new Socket(); //socket.setKeepAlive(true); SocketAddress socketAddress = new InetSocketAddress(slave.getIp(), slave.getPort()); socket.connect(socketAddress, 1000); //某些服务器ping延迟高时要增加,否则会报错connect timeout dataOutputStream = new DataOutputStream(socket.getOutputStream()); dataInputStream = new DataInputStream(socket.getInputStream()); log.info("条码扫描仪连接成功 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); } catch (Exception e) { socket = null; log.error("条码扫描仪连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); return false; } return true; } @Override public void close() { try { if (null != dataOutputStream) { dataOutputStream.close(); } if (null != dataInputStream) { dataInputStream.close(); } if (null != socket && !socket.isClosed()) { 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) { return null; } 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); } 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 String getBarcode() { if (Cools.isEmpty(barcode)) { return null; } else if("NG".endsWith(barcode)) { return null; } else { return barcode.trim(); } } public static void main(String[] args) throws Exception { BarcodeThread barcodeThread = new BarcodeThread(new Slave()); barcodeThread.getSlave().setIp("192.168.2.150"); barcodeThread.getSlave().setPort(51236); boolean connect = barcodeThread.connect(); System.out.println(connect); barcodeThread.write("T".getBytes(), "T".length()); byte[] read = barcodeThread.read(11, 1); System.out.println(new String(read)); } }