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