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

📄 messageencoder.java

📁 利用Java Socket写的一段通讯协议
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.ict.netcom2.message;

import java.io.*;
import java.nio.*;
import java.net.*;
import com.ict.netcom2.task.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class MessageEncoder {
    /**
     * Message sequence ID
     */
    int msgId = 0;


    /**
     * 生成消息NetPro_Connect(NetCom->NetPro),请求建立连接
     *
     * @param version int
     * 双方协商的版本号
     *
     * @param key String
     * 由NetCom指定的团体字
     *
     * @return byte[]
     */
    public byte[] encodeConnect(int version, String comm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);

        int len = 12 + 2 + comm.length();
        msgId++;
        try {
            dos.writeInt(len);
            dos.writeInt(CommandID.NetPro_Connect);
            dos.writeInt(msgId);
            dos.writeByte(version);
            dos.writeByte(comm.length());
            dos.writeBytes(comm);
            dos.close();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baos.toByteArray();
    }

    /**
     * 生成消息NetPro_Connect_ACK,请求建立连接应答
     *
     * @param status int
     * 0: 请求连接成功;
     * 1: 消息结构错;
     * 2: 版本不匹配;
     * 3: 团体字不匹配;
     * 4: 其他错误.
     *
     * @return byte[]
     */
    public byte[] encodeConnectAck(int status) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);

        int len = 12 + 1;
        msgId++;
        try {
            dos.writeInt(len);
            dos.writeInt(CommandID.NetPro_Connect_ACK);
            dos.writeInt(msgId);
            dos.writeByte(status);
            dos.close();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baos.toByteArray();
    }


    /**
     * 生成消息NetPro_Disconnect, 请求中止连接
     * @return byte[]
     */
    public byte[] encodeDisconnect() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);

        int len = 12;
        msgId++;
        try {
            dos.writeInt(len);
            dos.writeInt(CommandID.NetPro_Disconnect);
            dos.writeInt(msgId);
            dos.close();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baos.toByteArray();
    }

    /**
     * 生成消息NetPro_Disconnect_ACK,请求中止连接应答<br>
     * Useless.
     * @return byte[]
     */
    public byte[] encodeDisconnectAck() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);

        int len = 12;
        msgId++;
        try {
            dos.writeInt(len);
            dos.writeInt(CommandID.NetPro_Disconnect_ACK);
            dos.writeInt(msgId);
            dos.close();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baos.toByteArray();
    }

    /*
    public byte[] encodeConnect(int netproId, int version, String key) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);

        int len = 12 + 6 + key.length();
        msgId++;
        try {
            dos.writeInt(len);
            dos.writeInt(CommandID.NetPro_Connect);
            dos.writeInt(msgId);
            dos.writeInt(netproId);
            dos.writeByte(version);
            dos.writeByte(key.length());
            dos.writeBytes(key);
            dos.close();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baos.toByteArray();
    }
    */

    /**
     * 生成消息NetCom_Register,请求注册NetCom。
     * NetPro会给所有注册的NetCom发送状态变迁消息和事件告警通知。
     *
     * @param netproId int
     * NetPro的ID号
     *
     * @param ipVer int
     * 所用ip协议的颁布号
     *
     * @param netcomIp String
     * NetCom的IP地址
     *
     * @return byte[]
     */
    public byte[] encodeRegister(int netproId, String netcomIp) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);

        InetAddress ia = null;
        try {
        	ia = InetAddress.getByName(netcomIp);
        } catch (UnknownHostException e) {
        	e.printStackTrace();
        }
        if (ia == null) 
        	return null;
        
        byte[] b = ia.getAddress();
        if (b.length == 4) {
            int len = 12 + 12;
            msgId++;
            try {
                dos.writeInt(len);
                dos.writeInt(CommandID.NetCom_Register);
                dos.writeInt(msgId);
                dos.writeInt(netproId);
                dos.writeInt(Integer.parseInt("02000000", 16));
                dos.write(b);
                dos.close();
                baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (b.length == 16) {
            int len = 12 + 24;
            msgId++;
            try {
                dos.writeInt(len);
                dos.writeInt(CommandID.NetCom_Register);
                dos.writeInt(msgId);
                dos.writeInt(netproId);
                dos.writeInt(10);
                dos.write(b);
                dos.close();
                baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return baos.toByteArray();
    }

    public byte[] encodeUnRegister(String netcomIp) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        InetAddress ia = null;
        try {
        	ia = InetAddress.getByName(netcomIp);
        } catch (UnknownHostException e) {
        	e.printStackTrace();
        }
        if (ia == null) 
        	return null;
        
        byte[] b = ia.getAddress();
        if (b.length == 4) {
            int len = 12 + 8;
            msgId++;
            try {
                dos.writeInt(len);
                dos.writeInt(CommandID.NetCom_unRegister);
                dos.writeInt(msgId);
                dos.writeInt(Integer.parseInt("02000000", 16));
                dos.write(b);
                dos.close();
                baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (b.length == 16) {
            int len = 12 + 20;
            msgId++;
            try {
                dos.writeInt(len);
                dos.writeInt(CommandID.NetCom_unRegister);
                dos.writeInt(msgId);
                dos.writeInt(10);
                dos.write(b);
                dos.close();
                baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return baos.toByteArray();
    }

    public byte[] encodeLivingCheck() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        int len = 12;
        msgId++;
        try {
            dos.writeInt(len);
            dos.writeInt(CommandID.NetPro_LivingCheck);
            dos.writeInt(msgId);
            dos.close();

⌨️ 快捷键说明

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