Java TCP服务端多线程接收RFID网络读卡器上传数据

Java TCP服务端多线程接收RFID网络读卡器上传数据_第1张图片

本示例使用设备介绍:WIFI/TCP/UDP/HTTP协议RFID液显网络读卡器可二次开发语音播报POE-淘宝网 (taobao.com)

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;


public class TCPServer_Java {
    //监听端口
    private static final int PORT = 39169;

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = null;
        Socket socket = null;
        try {
            //建立服务器的Socket,并设定一个监听的端口PORT
            serverSocket = new ServerSocket(PORT);
            //由于需要进行循环监听,因此获取消息的操作应放在一个while大循环中
            while(true){
                try {
                    //建立跟客户端的连接
                    socket = serverSocket.accept();
                    ServerThread thread = new ServerThread(socket);
                    thread.start();
                } catch (Exception e) {
                    System.out.println("建立与客户端的连接出现异常");
                    e.printStackTrace();
                }
                //ServerThread thread = new ServerThread(socket);
                //thread.start();
            }
        } catch (Exception e) {
            System.out.println("端口被占用");
            e.printStackTrace();
        }
        finally {
            serverSocket.close();
        }
    }
}

//服务端线程类
//继承Thread类的话,必须重写run方法,在run方法中定义需要执行的任务。
class ServerThread extends Thread {
    private Socket socket ;
    InputStream inputStream;
    OutputStream outputStream;

    public ServerThread(Socket socket){
        this.socket=socket;
    }

    public void run(){
        try {
            while (true){
                //接收客户端的消息,解析信息并回应读卡器
                //System.out.println(socket);
                byte[] bytes =new byte[1024];
                inputStream =socket.getInputStream();
                int GetDataLen=0;
                while ((GetDataLen=inputStream.read(bytes))!=-1){       //通过这个方法读取全部数据 及 长度,
                    break;
                }

                if(GetDataLen>0) {
                    String bytestr = "";
                    String DataStr = "";
                    String DispStr = "";
                    String CardNo16 = "";
                    String SerialNum = "";
                    long cardno10;
                    long cardnum;

                    for (int p = 0; p < GetDataLen; p++) {
                        bytestr = "00" + Integer.toHexString(bytes[p] & 0xff);
                        DataStr = DataStr + bytestr.substring(bytestr.length() - 2, bytestr.length()) + " ";
                    }
                    System.out.println("Received from " + socket.getRemoteSocketAddress() + " : " + DataStr);

                    switch (bytes[0]) {
                        case (byte)0xc1:
                        case (byte)0xcf:
                            if(bytes[0]==(byte)0xc1){
                                DispStr = "数据解析:接收到读取IC卡号信息,";
                            }else{DispStr = "数据解析:接收到IC卡离开读卡器,";}
                            DispStr = DispStr + "功能码:" + Integer.toHexString(bytes[0] & 0xff);
                            DispStr = DispStr + ",设备IP:" + Integer.toString(bytes[1] & 0xff) + "." + Integer.toString(bytes[2] & 0xff) + "." + Integer.toString(bytes[3] & 0xff) + "." + Integer.toString(bytes[4] & 0xff);
                            DispStr = DispStr + ",机号:" + Integer.toString((bytes[5] & 0xff) + ((bytes[6] & 0xff) * 256));
                            DispStr = DispStr + ",数据包序号:" + Integer.toString((bytes[7] & 0xff) + ((bytes[8] & 0xff) * 256));
                            int Cardlen = bytes[9];
                            DispStr = DispStr + ",卡号长度:" + Integer.toString(Cardlen);
                            CardNo16 = "";
                            for (int p = 10; p < 10 + Cardlen; p++) {
                                bytestr = "00" + Integer.toHexString(bytes[p] & 0xff);
                                CardNo16 = CardNo16 + bytestr.substring(bytestr.length() - 2, bytestr.length());
                            }
                            DispStr = DispStr + ",16进制卡号:" + CardNo16;
                            cardnum = bytes[10] & 0xff;
                            cardnum = cardnum + (bytes[11] & 0xff) * 256;
                            cardnum = cardnum + (bytes[12] & 0xff) * 65536;
                            cardnum = cardnum + (bytes[13] & 0xff) * 16777216;
                            cardno10 = 0;
                            for (int j = 28; j >= 0; j -= 4) {
                                cardno10 = cardno10 << 4 | (cardnum >>> j & 0xF);
                            }
                            DispStr = DispStr + ",转10进制卡号:" + String.format("%010d", cardno10);

                            SerialNum = "";
                            for (int p = 10 + Cardlen; p < GetDataLen; p++) {
                                bytestr = "00" + Integer.toHexString(bytes[p] & 0xff);
                                SerialNum = SerialNum + bytestr.substring(bytestr.length() - 2, bytestr.length());
                            }
                            DispStr = DispStr + ",唯一硬件序号:" + SerialNum;
                            System.out.println(DispStr + "\n");

                            //向客户端发送消息
                            byte[] RespByte=GetResponseData(2); //生成不同的回应数据包

                            outputStream = socket.getOutputStream();
                            outputStream.write(RespByte);
                            bytestr = "";
                            DataStr = "";
                            for (int p = 0; p < RespByte.length; p++) {
                                bytestr = "00" + Integer.toHexString(RespByte[p] & 0xff);
                                DataStr = DataStr + bytestr.substring(bytestr.length() - 2, bytestr.length()) + " ";
                            }
                            System.out.println("Send Data To "+socket.getRemoteSocketAddress()+" : "+DataStr+"\n");
                            break;

                        case (byte)0xd1:
                        case (byte)0xdf:
                            if(bytes[0]==(byte)0xd1){
                                DispStr="数据解析:接收到读取ID卡号信息,";
                            }else{DispStr="数据解析:接收到ID卡离开读卡器,";}
                            DispStr=DispStr+"功能码:"+ Integer.toHexString(bytes[0] & 0xff);
                            DispStr=DispStr+",设备IP:"+  Integer.toString(bytes[1] & 0xff) + "." + Integer.toString(bytes[2] & 0xff) + "." + Integer.toString(bytes[3] & 0xff) + "." + Integer.toString(bytes[4] & 0xff);
                            DispStr=DispStr+",机号:"+  Integer.toString((bytes[5] & 0xff) + ((bytes[6] & 0xff) * 256));
                            DispStr=DispStr+",数据包序号:"+ Integer.toString((bytes[7] & 0xff) + ((bytes[8] & 0xff) * 256));
                            CardNo16="";
                            for (int p = 9; p < 14; p++) {
                                bytestr = "00" + Integer.toHexString(bytes[p] & 0xff);
                                CardNo16 = CardNo16 + bytestr.substring(bytestr.length() - 2, bytestr.length());
                            }
                            DispStr=DispStr+",16进制卡号:"+CardNo16;

                            cardnum = bytes[9] & 0xff;
                            cardnum = cardnum + (bytes[10] & 0xff) * 256;
                            cardnum = cardnum + (bytes[11] & 0xff) * 65536;
                            cardnum = cardnum + (bytes[12] & 0xff) * 16777216;
                            cardno10 = 0;
                            for (int j = 28; j >= 0; j -= 4) {
                                cardno10 = cardno10 << 4 | (cardnum >>> j & 0xF);
                            }
                            DispStr=DispStr+",转10进制卡号:"+ String.format("%010d", cardno10);

                            SerialNum="";
                            for (int p = 14; p < GetDataLen; p++) {
                                bytestr = "00" + Integer.toHexString(bytes[p] & 0xff);
                                SerialNum = SerialNum + bytestr.substring(bytestr.length() - 2, bytestr.length());
                            }
                            DispStr=DispStr+",唯一硬件序号:"+ SerialNum;
                            System.out.println(DispStr+"\n");

                            //向客户端发送消息
                            RespByte=GetResponseData(2); //生成不同的回应数据包

                            outputStream = socket.getOutputStream();
                            outputStream.write(RespByte);
                            bytestr = "";
                            DataStr = "";
                            for (int p = 0; p < RespByte.length; p++) {
                                bytestr = "00" + Integer.toHexString(RespByte[p] & 0xff);
                                DataStr = DataStr + bytestr.substring(bytestr.length() - 2, bytestr.length()) + " ";
                            }
                            System.out.println("Send Data To "+socket.getRemoteSocketAddress()+" : "+DataStr+"\n");
                            break;

                        case (byte)0xf3:
                            DispStr="数据解析:接收到设备心跳包,";
                            DispStr=DispStr+"功能码:"+Integer.toHexString(bytes[0] & 0xff);
                            DispStr=DispStr+",设备IP:"+Integer.toString(bytes[1] & 0xff)+"."+Integer.toString(bytes[2] & 0xff)+"."+Integer.toString(bytes[3] & 0xff)+"."+Integer.toString(bytes[4] & 0xff);
                            DispStr=DispStr+",机器号:"+Integer.toString((bytes[5] & 0xff)+((bytes[6] & 0xff) *256));
                            DispStr=DispStr+",包序号:"+Integer.toString((bytes[7] & 0xff)+((bytes[8] & 0xff) *256));
                            DispStr=DispStr+",心跳码:"+Integer.toHexString(bytes[9] & 0xff);
                            DispStr=DispStr+",长  度:"+Integer.toHexString(bytes[10] & 0xff);
                            DispStr=DispStr+",继电器:"+Integer.toHexString(bytes[11] & 0xff);
                            DispStr=DispStr+",按键值:"+Integer.toHexString(bytes[12] & 0xff);
                            for(int p=13;p<17;p++){
                                bytestr="00"+Integer.toHexString(bytes[p] & 0xff);
                                SerialNum=SerialNum+ bytestr.substring(bytestr.length() -2,bytestr.length());
                            }
                            DispStr=DispStr+",随机码:"+SerialNum;
                            SerialNum="";
                            for(int p=17;p

 

你可能感兴趣的:(网络读卡器,18002295132,QQ:954486673,java,TCP,Server,多线程,服务端)