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

public class Server {
    //监听端口
    private static final int PORT = 5000;

    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();
                 } catch (Exception e) {
                     System.out.println("建立与客户端的连接出现异常");
                     e.printStackTrace();
                 }
                 ServerThread thread = new ServerThread(socket);
                 thread.start();
            }
        } catch (Exception e) {
            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);
                inputStream=socket.getInputStream();
                byte[] bytes = new byte[1024];
                int len =  inputStream.read(bytes);
                byte[] rfidbtye = new byte[len];
                for(int i = 0;i < len;i++){
                	rfidbtye[i] = bytes[i];
                }
                String rfid = byteToString(rfidbtye);  
                //CRC
            	int[] data = Crc16Util.hexString2IntBytes(rfid);
        	    int res = Crc16Util.crc16(data);
        		if(res == 0)
        		{
        			//0C00EE0001041234567849D8BB
        			//System.out.print(rfid);
        		 //epc
        		   String Lepc = rfid.substring(12, rfid.length() - 6);
        		   System.out.print(Lepc);
        		}
            }
        } catch (Exception e) {
            //System.out.println("客户端主动断开连接了");
            e.printStackTrace();
        }
        //操作结束，关闭socket
        try{
            socket.close(); 
        }catch(IOException e){
            System.out.println("关闭连接出现异常");    
            e.printStackTrace();
        }
    }
    public String byteToString(byte[] b){
        StringBuffer sb = new StringBuffer("");
        for(int i = 0; i < b.length; i++){
            String temp=Integer.toHexString(b[i] & 0xff);
            if(temp.length()==1)
            {
                temp="0"+temp;
            }
            sb.append(temp);
        }
        return sb.toString().toUpperCase();
}
}