基于CMPP2.0的Socket客户端(Java)

短信验证码,短信通知等功能均可通过此Socket客户端实现。

注意事项如下:

1、服务器端是按字节依次读取的,所以客户端发送的字节包中的字段要严格按照CMPP协议中的顺序,而且每个字段都要有。

2、每个字段的实际长度不足时,用0补齐;电话号码后用空格补齐

3、使用ByteBuffer时,要注意putInt()和put()的区别


我亲身实践出来的模板如下,供大家参考学习:

public class Login {

 public static int pktLen;
   public static int reqId;
   public static int seqId;
   public static byte[] source_Addr = new byte[6];
   public static byte[] authenticatorSource = new byte[16];
   public static int timeStamp;
   public static byte version;

   private String strtimeStamp;
   private String loginPwd="xxxxxxxx";
   private String spId="123456";
   private String spPwd;
   
 public Login(int i)throws UnsupportedEncodingException,NoSuchAlgorithmException {
     pktLen = 39;
     reqId = 0x00000001;
     version = 0x10;
     seqId = i;
     source_Addr=spId.getBytes();
     SimpleDateFormat sf =  new SimpleDateFormat("MMddHHmmss");
     strtimeStamp = sf.format(new Date());
     timeStamp = Integer.valueOf(strtimeStamp).intValue();
     byte[] testa={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
     spPwd=spId+new String(testa)+loginPwd+strtimeStamp;
     authenticatorSource=Login.Md5(spPwd);
 }
  
 
  public static void main(String[] args) throws UnknownHostException, IOException, NoSuchAlgorithmException{
   Socket socket =null; 
   socket = new Socket("2xx.6x.10x.19x", 9890);   
       new Login(1);
       OutputStream os = socket.getOutputStream();  
    os.write(Login.toBytes());
    os.flush();
    InputStream inputStream = socket.getInputStream();  
              
    if((inputStream.read())==0)  
                  {  
                    System.out.println("login successfully");                     
                  }  
    
   os.write(Login.messageToBytes());
   os.flush();
   int a =0;
   inputStream.skip(25);
   while((a=inputStream.read())!=-1){
    System.out.println("RESULT: "+a);  
   }
  }

 private static byte[] Md5(String str)throws NoSuchAlgorithmException, UnsupportedEncodingException {
   MessageDigest md5 = MessageDigest.getInstance("MD5");
          md5.update(str.getBytes("UTF8"));
          byte[] temp;
          temp = md5.digest("".getBytes("UTF8"));
   return  temp; 
 }
 
 public static byte[] toBytes(){
  byte[] b = new byte[39];
     ByteBuffer bb = ByteBuffer.wrap(b, 0, 39);
     bb.order(ByteOrder.BIG_ENDIAN);
     bb.putInt(pktLen);
     bb.putInt(reqId);
     bb.putInt(seqId);
     bb.put(source_Addr);
     bb.put(authenticatorSource);
     bb.put(version);
     bb.putInt(timeStamp);
    
     return b;

 }
 public static byte[] messageToBytes(){
    int Total_Length=171;
    int Command_Id =4;
    int Sequence_Id = 2;
    byte[] Msg_id=new byte[8];
    byte PK_total=1;
    byte PK_number=1;
    byte Registered_Delivery=1;
    String Service_Id = "testmsg111";
    byte Msg_level=1;
    byte Fee_UserType=0;
    byte[] Fee_terminal_Id=new byte[21];
    byte TP_pId=0;
    byte TP_udhi=0;
    byte Msg_Fmt=0;
    byte[] Msg_src=new byte[6];
    byte[] FeeType=new byte[2];
    byte[] FeeCode=new byte[6];
    String ValId_Time = "00000000000000000";
    String At_Time = "00000000000000000";
    byte[] Src_Id=new byte[21];
    byte DestUsr_tl;
    byte[] Dest_terminal_Id=new byte[21];
    byte Msg_Length;
    byte[] Msg_Content;
    byte[] Reserve=new byte[8];
    Msg_src="200030".getBytes();
    FeeType="01".getBytes();
    FeeCode="000010".getBytes();
    Src_Id="1069034531           ".getBytes();
    DestUsr_tl=1;
    Dest_terminal_Id="18511790624          ".getBytes();
    Msg_Length=12;
    Msg_Content="digitalchina".getBytes();
    byte[] b = new byte[171];
    ByteBuffer bb = ByteBuffer.wrap(b, 0, 171);
    bb.order(ByteOrder.BIG_ENDIAN);
    bb.putInt(Total_Length);
    bb.putInt(Command_Id);
    bb.putInt(Sequence_Id);
    bb.put(Msg_id);
    bb.put(PK_total);
    bb.put(PK_number);
    bb.put(Registered_Delivery);
    bb.put(Msg_level);
    bb.put(Service_Id.getBytes());
    bb.put(Fee_UserType);
    bb.put(Fee_terminal_Id);
    bb.put(TP_pId);
    bb.put(TP_udhi);
    bb.put(Msg_Fmt);
    bb.put(Msg_src);
    bb.put(FeeType);
    bb.put(FeeCode);
    bb.put(ValId_Time.getBytes());
    bb.put(At_Time.getBytes());
    bb.put(Src_Id);
    bb.put(DestUsr_tl);
    bb.put(Dest_terminal_Id);
    bb.put(Msg_Length);
    bb.put(Msg_Content);
    bb.put(Reserve);
    bb.position();
    return b;
 }
 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值