读书人

与C通讯2

发布时间: 2012-10-11 10:16:10 作者: rapoo

与C通信2

协议都是双方统一的

java端定义的报文

?

?

?

package cn.com.henan.socket;import java.util.Calendar; //此类试图将所有的通讯协议的编解码都包含在内!按照一次交互的过程来区分public class newsDetail implements InewsDetail{protected intnetErr;//用来表示是不是网络出错了,主要是超时。这个时候就不需要检查其他参数了。protected inttype;//就是对应具体的操作类型码protected byte[]StreamID=new byte[16];//对应具体的流水号protected byte[]asyn = new byte[2];//这个还是有问题 不能达到预计效果 需要修改static private int seq=0;//生成流水号后2位的时候使用的static private CalendarlastCa;public newsDetail() {getStreamID();}public int getType() {return type;}public void setType(int type) {this.type = type;}//基类中的编解码函数没有作用,具体使用的编解码函数在各个子类中需要重新实现//必须有返回值 因为调用者需要根据返回值做一些操作//这里的参数 buf是需要填写的缓冲区  start 是缓冲区开始位置 len 是可用的缓冲区长度public int encode(byte[] buf,int start,int len){return 0;}//这里的参数 buf是需要需要解码的缓冲区  start 是缓冲区开始位置 len 是需要解码的长度public int decode(byte[] buf,int start,int len){return 0;}public void getStreamID(){Calendar ca = Calendar.getInstance();      int year = ca.get(Calendar.YEAR);//获取年份      int month=ca.get(Calendar.MONTH)+1;//获取月份       int day=ca.get(Calendar.DATE);//获取日      int minute=ca.get(Calendar.MINUTE);//分       int hour=ca.get(Calendar.HOUR);//小时       int second=ca.get(Calendar.SECOND);//秒      int am_pm=ca.get(Calendar.AM_PM);      if (am_pm==Calendar.PM)      hour += 12;      if (hour>=24)      hour -= 24;          System.out.println(seq);             if (lastCa!=ca)      {      lastCa = ca;      seq = 12;      }      else      {      seq++;      if (seq>=100)      seq = 0;      }            //现在根据上面的字段组成StreamID字符串      //目前先使用手工的办法来写,效率高一些           StreamID[0] = (byte)(year/1000+'0');      StreamID[1] = (byte)((year-(StreamID[0]-'0')*1000)/100+'0');      StreamID[2] = (byte)((year-(StreamID[0]-'0')*1000-(StreamID[1]-'0')*100)/10+'0');      StreamID[3] = (byte)(year-(StreamID[0]-'0')*1000-(StreamID[1]-'0')*100-(StreamID[2]-'0')*10+'0');            StreamID[4] = (byte)(month/10+'0');      StreamID[5] = (byte)((month-(StreamID[4]-'0')*10)+'0');            StreamID[6] = (byte)(day/10+'0');      StreamID[7] = (byte)((day-(StreamID[6]-'0')*10)+'0');            StreamID[8] = (byte)(hour/10+'0');      StreamID[9] = (byte)((hour-(StreamID[8]-'0')*10)+'0');            StreamID[10] = (byte)(minute/10+'0');      StreamID[11] = (byte)((minute-(StreamID[10]-'0')*10)+'0');            StreamID[12] = (byte)(second/10+'0');      StreamID[13] = (byte)((second-(StreamID[12]-'0')*10)+'0');            StreamID[14] = (byte)(seq/10+'0');      StreamID[15] = (byte)((seq-(StreamID[14]-'0')*10)+'0');               System.out.println("现在时间");      System.out.println("用Calendar.getInstance().getTime()方式显示时间: " + ca.getTime());      System.out.println("用Calendar获得日期是:" + year +"年"+ month +"月"+ day + "日");            System.out.println("用Calendar获得时间是:" + hour +"时"+ minute +"分"+ second +"秒");      }public static void main(String[] args) {{newsDetail nn1 = new newsDetail();}try {Thread.currentThread().sleep(3000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();} {newsDetail nn2 = new newsDetail();}      }}

?

?

?

?

?

package cn.com.henan.socket;//针对操作类型码0x05 主要是发送彩信给指定用户使用的 add by ysenpublic class newsDetailMMS extends newsDetail {//发送方设置private String   mmsfile;//需要发送的文件名private int    mmsfileL;//彩信内容长度private int      isdnnum;//接受方号码总数private String[] isdn;    //接受方号码 //接受方解码设置private intresult;//接受结果private intrecvisdnnum;//实际接收的号码数private String   resultMessage;//操作解析的结果public newsDetailMMS() {super();}public int encode(byte[] buf,int start,int len){//如果可用缓冲区小于最小字节要求 直接退出if(len<21)return -1;int step=start;System.out.println("This is a newsDetailMMS encode!");buf[step] = (byte)0x88;step++;buf[step] = (byte)0x88;step++;//长度 先预留 最后填写step+=2;for (int i=0;i<16;i++)buf[step+i] = this.StreamID[i];step += 16;buf[step] = (byte)(this.type);step ++;buf[step] = (byte)(this.mmsfileL);step ++;for(int i=0;i<this.mmsfileL;i++){buf[step++]=this.mmsfile.getBytes()[i]; }buf[step] = (byte) isdnnum;step ++;for(int i=0;i<this.isdnnum;i++){for(int num=0;num<11;num++){buf[step++]=this.isdn[i].getBytes()[num];}}//最后填写出长度字段 step-start-20  buf[start+2] =  (byte)((step-start-20)&0xff);buf[start+3] =  (byte)(((step-start-20)>>8)&0xff);return step;}//如果返回0  表示解码数据还不完整 正数就是解码的字节数 负数表示解码出错public int decode(byte[] buf,int start,int len){int step=0;intltype;//就是对应具体的操作类型码byte[]lStreamID = new byte[16];byte[]lasyn = new byte[2];intdatalen=0;System.out.println("This is a newsDetailMMS decode!");//如果头部还没有接受完整就不解码if (len<=20)return 0;lasyn[0] = buf[step];step++;lasyn[1] = buf[step];step++;//如果同步码不正确 后面就不需要解码了if ((lasyn[0]!=(byte)0x88)||(lasyn[1]!=(byte)0x88))return -1;datalen = (buf[step]&0xff)+(buf[step+1]<<8)&0xff00;step += 2;//如果数据部分还没有接受完全 也要退出if (datalen+20>len)return 0;for (int i=0;i<16;i++){lStreamID[i] = buf[step+i];//如果STREAMID不一样 出错退出if (lStreamID[i]!=this.StreamID[i])return -1;}step += 16;ltype = buf[step];step++;if (ltype!=this.type)return -1;result = buf[step];step++;recvisdnnum = buf[step];step++;return step;}public String getResultMessage() {switch(this.result){case(1):this.resultMessage="成功,完整接收到所有号码";break;case(2):this.resultMessage="成功,只接收到部分号码";break;case(3):this.resultMessage="失败,彩信文件不存在";break;default:this.resultMessage="失败原因未知";}return resultMessage; }public void setResultMessage(String resultMessage) {this.resultMessage = resultMessage;}public String getMmsfile() {return mmsfile;}public void setMmsfile(String mmsfile) {this.mmsfile = mmsfile;}public int getMmsfileL() {return mmsfileL;}public void setMmsfileL(int mmsfileL) {this.mmsfileL = mmsfileL;}public int getIsdnnum() {return isdnnum;}public void setIsdnnum(int isdnnum) {this.isdnnum = isdnnum;}public String[] getIsdn() {return isdn;}public void setIsdn(String[] isdn) {this.isdn = isdn;}public int getResult() {return result;}public void setResult(int result) {this.result = result;}public int getRecvisdnnum() {return recvisdnnum;}public void setRecvisdnnum(int recvisdnnum) {this.recvisdnnum = recvisdnnum;}public static void main(String[] args) {}}

?

?

?

?

?

?

读书人网 >软件架构设计

热点推荐