写网络程序,协议与协议解析如何设计呢?
之间的关系做到程序协议无关性

解决方案 »

  1.   

    所有的信息尽量一条一条的发,不要整合在一起,不然不好扩展
    每一条信息应该有包头和包体
    包头定义我举个例给你
    前两个固定(比如0XFF,0XFA),然后是命令号(表示哪个哪条命令的回应,一个交互,两端的命令号要一致),消息类型,消息长度,等。
    包体就是具体信息
    最后还应该有个效验码(验证消息可靠性)。
      

  2.   

    给你个代码片段:// 前置初始化工作
    // 数据输出流
    DataOutputStream out = new DataOutputStream(socket.getOutputStream());
    // 数据输入流
    DataInputStream in = new DataInputStream(socket.getInputStream());
    //.....// 发送版本号
    out.writeByte(protocol.getVersion());
    // 发送服务器标示
    out.write(ServerBean.getInstance().getServerGuid()
    .getBytes("UTF-8"));
    // 数据包类型
    out.writeShort(protocol.getDataType());
    // 发送数据域
    int length = 0;
    byte[] content_utf8 = null;
    if (protocol.getContent() != null) {
    content_utf8 = protocol.getContent();
    length = content_utf8.length;
    out.writeInt(length);
    } else
    out.writeInt(length); // 发送加密类型
    out.writeByte(protocol.getEncrpytionType());
    // 发送校验和
    out.writeInt(this.getCheckSum(content_utf8));
    // 发送数据内容
    if (length != 0)
    out.write(content_utf8);
    protocol是协议Bean,所有的协议信息封装在Bean里。
    然后对应的是接收端:Protocol data = null;
    // 读取版本号
    byte version = in.readByte();
    // 读取guid
    byte[] guid = new byte[36];
    in.read(guid);
    // 读取数据包类型
    short dataType = in.readShort();
    // 读取内容长度
    int contentLength = in.readInt();
    // 读取加密类型
    byte encrpytionType = in.readByte();
    // 读取检验和
    int checkSum = in.readInt();
    if (contentLength < 0)
    return null; byte[] content = null;
    if (contentLength >= 0) {
    // 内容
    content = new byte[contentLength];
    int left = contentLength;
    int received = 0;
    int off = 0;
    // 当没有读到文件结尾,继续读
    while (left > 0 && received != -1) {
    received = in.read(content, off, left);
    left -= received;
    off += received;
    }
    }
    data = new Protocol(version, new String(guid), dataType,
    contentLength, encrpytionType, checkSum,content);
    上面代码是基于socket的阻塞模式下的收取跟发送的代码片段。
    你可以定义每个协议片段的长度,比如
    // 读取guid
    byte[] guid = new byte[36];
    in.read(guid);
    规定必须是36位的byte,如果发送的协议不正确,则接收端会超时报错。封装这个错误,向上层抛出就行了。
      

  3.   

    自己设计协议,定义协议头,协议版本号,协议校验位,协议体...等等内容,然后再按前面的接受跟发送协议的代码片段进行收发。顶多再加个校验位,在接受的时候用校验位的内容对你需要校验的区域进行校验。比如内容区长度校验,数据包类型校验等等。如果校验不通过,采取丢包或者请求重发操作。上层代码只操作的是Protocol的Bean,将你要发送的内容放入内容区,以Bean的形式传递给下层通讯层。下层的通讯层帮你完成收发工作。如果要扩展协议,只用在通讯层做改动。变更接受和发送方法即可。你可以在Bean中预留扩充字段,进行扩充预留。
      

  4.   

    你可以根据你自己的需要,基于socket通讯,还是基于NIO通讯。只用更改通讯层即可,上层业务与下层通讯定义好数据交换接口。剩下的你可以保证接口不变,随意更换下层通讯实现