}
if (ss==timeout)
{
break;
}
r_buff.flip();
//判断recvBuf能不能放下接收到的数据
if (r_buff.limit()+recvBufStart+recvBufLen>recvBufTotalLen)
{
//放不下了
//那就先看看前面是不是有空余
if (recvBufStart>0)
{
for(int i=0;i
recvBuf = recvBuf[i+recvBufStart];
}
recvBufStart = 0;
}
if (r_buff.limit()+recvBufStart+recvBufLen>recvBufTotalLen)
{
//这个时候就是真的说数据区长度不够了,属于致命错误
System.err.println("致命错误! 缓冲区长度过小!");
}
}
else
{ // 也可以转化为字符串,不过需要借助第三个变量了。
r_buff.get(recvBuf,recvBufStart+recvBufLen,r_buff.limit());
//得到了一次数据就要试着做一次解码,如果能够解码,那就完成解码,不能则表示数据不完整,继续等待新数据
//这里注意返回值 如果是0 表示数据不完整 如果是正数 就是解码的字节数 负数表示解码出错
recvBufLen += r_buff.limit();
if (decodeBufToPara(nD)!=0)
break;
}
System.out.println("reply is " + r_buff.limit() + " long " );
}
}
sc.socket().close();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (InterruptedException ie) {
}
System.out.println("Exit App....... " );
}
public static void main(String[] args)
{
newsDetailNewsSum nDNS = new newsDetailNewsSum();
newsSockBase nsb = new newsSockBase();
nsb.setIPandPort("192.168.0.106",8888);
nsb.start(nDNS);
System.out.println("Exit Allllll....... " );
}
}
下面是报文协议的基类
//此类试图将所有的通讯协议的编解码都包含在内!按照一次交互的过程来区分
public class newsDetail implements InewsDetail
{
protected int netErr; //用来表示是不是网络出错了,主要是超时。这个时候就不需要检查其他参数了。
protected int type; //就是对应具体的操作类型码
protected byte[] StreamID=new byte[16]; //对应具体的流水号
protected byte[] asyn = new byte[2];
//这个还是有问题 不能达到预计效果 需要修改
static private int seq=0; //生成流水号后2位的时候使用的
static private Calendar lastCa;
public newsDetail()
{
getStreamID();
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
//基类中的编解码函数没有作用,具体使用的编解码函数在