⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 messageencoder.java

📁 利用Java Socket写的一段通讯协议
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baos.toByteArray();
    }

    public byte[] encodeSetComm(String old, String newComm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        int len = 12 + 2 + old.length() + newComm.length();
        msgId++;
        try {
            dos.writeInt(len);
            dos.writeInt(CommandID.NetPro_SetComm);
            dos.writeInt(msgId);
            dos.writeByte(old.length());
            dos.writeBytes(old);
            dos.writeByte(newComm.length());
            dos.writeBytes(newComm);
            dos.close();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baos.toByteArray();
    }

    public byte[] encodeQuerySysInfo() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        int len = 12;
        msgId++;
        try {
            dos.writeInt(len);
            dos.writeInt(CommandID.NetPro_QuerySysInfo);
            dos.writeInt(msgId);
            dos.close();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baos.toByteArray();
    }

    /**
     * 生成消息NetPro_QueryTask,查询系统任务信息
     * 
     * @param type int : 
     * 请求的任务列表类型:<p>
     * 0:所有任务队列<p>
     * 1:READY队列<p>
     * 2:RUNNING队列<p>
     * 
     * @return byte[]
     * 经过编码后的消息
     */
    public byte[] encodeQueryTask(int type) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        int len = 12 + 1;
        msgId++;
        try {
            dos.writeInt(len);
            dos.writeInt(CommandID.NetPro_QueryTask);
            dos.writeInt(msgId);
            dos.writeByte(type);
            dos.close();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baos.toByteArray();
    }
    
    /**
     * 生成消息NetPro_QueryThreads,查询线程信息
     * @return
     * byte[] : 编码后的消息
     */
    public byte[] encodeQueryThreads() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        int len = 12;
        msgId++;
        try {
            dos.writeInt(len);
            dos.writeInt(CommandID.NetPro_QueryThreads);
            dos.writeInt(msgId);
            dos.close();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baos.toByteArray();
    }

    public byte[] encodeAddTask(Task task) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        byte[] taskCode = task.getTaskCode();
        int len = 12 + taskCode.length;
        msgId++;
        try {
            dos.writeInt(len);
            dos.writeInt(CommandID.NetPro_TaskAdd);
            dos.writeInt(msgId);
            dos.write(taskCode);
            dos.close();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baos.toByteArray();    	
    }
    
    public byte[] encodeTaskStop(int taskId) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        int len = 12 + 4;
        msgId++;
        try {
            dos.writeInt(len);
            dos.writeInt(CommandID.NetPro_TaskStop);
            dos.writeInt(msgId);
            ByteBuffer bb = ByteBuffer.wrap(new byte[4]);
            bb.order(ByteOrder.LITTLE_ENDIAN);
            bb.putInt(taskId);
            dos.write(bb.array());
            dos.close();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baos.toByteArray();
    }

    /*
    public byte[] encodeTaskStatusNotify(int netproId, int taskId,
                                         int statusInfo) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        int len = 12 + 9;
        msgId++;
        try {
            dos.writeInt(len);
            dos.writeInt(CommandID.NetPro_TaskStatusNotify);
            dos.writeInt(msgId);
            dos.writeInt(netproId);
            dos.writeInt(taskId);
            dos.writeByte(statusInfo);
            dos.close();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baos.toByteArray();
    }

    public byte[] encodeEventNotify(int netproId, int priority, int eventCode,
                                    String value) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        int len = 12 + 10 + value.length();
        msgId++;
        try {
            dos.writeInt(len);
            dos.writeInt(CommandID.NetPro_EventNotify);
            dos.writeInt(msgId);
            dos.writeInt(netproId);
            dos.writeByte(priority);
            dos.writeByte(eventCode);
            dos.writeInt(value.length());
            dos.writeBytes(value);
            dos.close();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baos.toByteArray();
    }
    */

    public byte[] encodeGetResult(Task task) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        String oid = task.getOid();
        int authCode = 0;
        int len = 12 + 5 + oid.length();
        msgId++;
        try {
            dos.writeInt(len);
            dos.writeInt(CommandID.NetPro_GetResult);
            dos.writeInt(msgId);
            dos.writeInt(authCode);
            dos.writeByte(oid.length());
            dos.writeBytes(oid);
            dos.close();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baos.toByteArray();
    }

    
    
    public static void main(String[] str) {
        /*
                 try {
            Socket skt = new Socket(InetAddress.getByName("10.21.2.30"), 161);
            BufferedReader br = new BufferedReader(new InputStreamReader(skt.
                    getInputStream()));
            PrintStream ps = new PrintStream(skt.getOutputStream());
            DataOutputStream dos = new DataOutputStream(new
                    BufferedOutputStream(skt.getOutputStream()));

            ByteArrayOutputStream ba = new ByteArrayOutputStream();
            DataOutputStream ds = new DataOutputStream(ba);

            int cmdID = Integer.parseInt("0000001", 16);
            int seqID = 1;
            String key = "justatest";
            int len = key.length() + 14;
            byte v = 4;
            ds.writeInt(len);
            ds.writeInt(cmdID);
            ds.writeInt(seqID);
            ds.writeByte(v);
            ds.writeByte(key.length());
            ds.writeBytes(key);
            ds.flush();
            
            ds.close();
            ba.close();
            System.out.println(ba);
            
            ps.write(ba.toByteArray());

            //br.readLine();

            //dos.close();
            //br.close();
            //skt.close();
                 } catch (IOException e) {
            e.printStackTrace();
                 }
		*/
    	/*
        ByteBuffer bb = ByteBuffer.wrap(new byte[4]);
        bb.order(ByteOrder.LITTLE_ENDIAN);
        bb.putInt(5);
        byte[] b = bb.array();
        System.err.print(b[0]);
        System.err.print(b[1]);
        System.err.print(b[2]);
        System.err.print(b[3]);
        
        bb.rewind();
        ByteBuffer bf = bb.order(ByteOrder.BIG_ENDIAN);
        b = bb.array();
        System.err.print(b[0]);
        System.err.print(b[1]);
        System.err.print(b[2]);
        System.err.print(b[3]);    
        */

    	String oid = "";
    		oid += TaskPredef.ACTIVE_MIB_ID_HEAD;
    	oid += "3.";
    	long id = Long.parseLong(Integer.toBinaryString(-1), 2);
    	oid += String.valueOf(id);
    	System.out.println(oid);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -