java socket网络通信多对多

 public static void serverStart() {

        try {
            int number = 1;
            ServerSocket s = new ServerSocket(SERVERPORT);
            System.out.println("服务器启动: " + s);
            while (true) {
                // 等待并接收请求,建立连接套接字。

                Socket incoming = s.accept();
                PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(incoming.getOutputStream())),true);
                out.write("CONNECT OK");
                out.flush();


                socketList.put(number,incoming);
                String error = "Connection" + number + " accepted:" + incoming;
                System.out.println("有客户端连接" + error);
                Thread t = new EchoThread(incoming, number);
                t.setDaemon(true);
                t.start();
                number++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}


class EchoThread extends Thread {

    private  int TIME_OUT=120000;//发送数据时间间隔120s
    private Socket s;

    private  int n;
    public EchoThread(Socket incoming,int number) {

        s = incoming;
        n = number;

    }
    public void run() {


        BufferedInputStream bis = null;
        PrintWriter out=null;
        try {
             bis = new BufferedInputStream(s.getInputStream());
            out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())),true);
            DataInputStream dis = new DataInputStream(bis);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();

            sleep(500);
            byte[] bytes = new byte[20];
            int arrlen=dis.available();
            int len;

            Date dt_0=new Date();
            long onceTime = dt_0.getTime();


            while((len=dis.read(bytes))!=-1){

                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                byteArrayOutputStream.write(bytes,0,len);
                byteArrayOutputStream.flush();
                byte [] temp =byteArrayOutputStream.toByteArray();
                bos=byteArrayOutputStream;


                System.out.println("-----");
                for(int i=0;i<temp.length;i++){
                 System.out.print(bytes[i]);
                }

                System.out.println("数组打印");

                if(temp.length==1&&bytes[0]==48){
                    System.out.println("心跳包判断跳出");
                    break;

                }else if(temp.length==10&&bytes[0]==(-86)&&bytes[9]==85){

                    System.out.println("数据帧判断跳出"+bytes[0]+bytes[9]);
                    break;

                }else if(temp.length==15){
                    System.out.println("IMEI判断跳出");
                    break;
                }

            }


            //判断时间是否超时
            long twiceTime=dt_0.getTime();
            if(twiceTime-onceTime>TIME_OUT){
                //客户端失联
                s.close();
            }else{
                onceTime=twiceTime;
            }


            bos.flush();

            //此处取得十进制数组
            byte [] data =bos.toByteArray();
            for(int i=0;i<data.length;i++){
                System.out.print(data[i]+"-");
            }
            System.out.println();
            if(data.length==15){
                                //IMEI存入数据库
                String IMEIstr=  bytestoString(data);
//                Long IMEI = Long.parseLong(IMEIstr);

                System.out.println("接收到IMEI码并返回0123"+IMEIstr);
                byte[] bt = {0x00, 0x01, 0x02, 0x03};
                out.write(new String(bt));
                out.flush();

            }else if(data.length==1&&data[0]==0){
                System.out.println("收到心跳包!");
            }else if(data.length==10&&bytes[0]==(-86)&&bytes[9]==85) {
                String temp= bytesToHexString(data);
                System.out.println(temp);

                //CRC校验
                String arr[] =temp.split(" ");

                byte [] CRCdata={data[1],data[2],data[3],data[4],data[5],data[6]};
                String CRCcode=CRC_16(CRCdata);
                String CRC=arr[7]+arr[8];

                if(CRCcode.equals(CRC)){
                    System.out.println("校验成功!");
                    if (arr[1].equals("15")) {
                        System.out.println("重发之前的命令!");

                    } else if (arr[1].equals("06")) {
                        System.out.println("正常数据");
                        if (arr[4].equals("00")) { }
                        else { }
                    }

                } else {
                    System.out.println("CRC校验与发送数据不符");
                }

            }else {
                System.out.println("数据格式错误!");
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值