当前位置:编程学习 > JAVA >>

东软cmppe发送长短信结果还是分了几条收到的问题

用的是东软的cmppe,Message mode设置为8,即代表这UDHI 设置为1. Data coding  也为8. 使用6位的消息头设置。 http://m.blog.csdn.net/blog/xian0617/5807542 参考了这个前辈的。  请大家帮忙 看看 。一会贴上代码。现在发完短信还是分为多条发到手机上。
运行的参数 args 数组是由手机号和内容组成。









import com.commerceware.cmpp.*;


import java.util.Date;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;

public class shortMsg {
  /**
   *
   * @param con
   * @return
   */
  protected String readPa(conn_desc con) {
    cmppe_result cr = null;
    CMPP cmpp = new CMPP();
    String ret = "";
    try {
      cr = cmpp.readResPack(con);
      switch (cr.pack_id) {
case CMPP.CMPPE_NACK_RESP:
  ret = "get nack pack";
  break;
case CMPP.CMPPE_LOGIN_RESP:
  cmppe_login_result cl;
  cl = (cmppe_login_result) cr;
  ret = "------------login resp----------: STAT = " +
     cl.stat;
  break;

case CMPP.CMPPE_LOGOUT_RESP:
  ret = "------------logout resp----------: STAT = " +
     cr.stat;
  break;

case CMPP.CMPPE_SUBMIT_RESP:
  cmppe_submit_result sr;
  sr = (cmppe_submit_result) cr;
  System.out.println("------------submit resp----------: STAT = " +
  sr.stat + " SEQ = " + sr.seq + "--us_count=" + sr.us_count + "---msg_id=" +
  new String(sr.msg_id));
  ret = sr.stat+"";
  break;

case CMPP.CMPPE_DELIVER:
  cmppe_deliver_result cd = (cmppe_deliver_result) cr;
  if (cd.status_rpt == 1)
    System.out.println( "Rpt status = " + cd.status_from_rpt);
  cmpp.cmpp_send_deliver_resp(con, cd.seq, cd.stat);
  ret = "Rpt status = " + cd.status_from_rpt;
  break;

case CMPP.CMPPE_CANCEL_RESP:
  ret = "---------cancel-----------: STAT = " + cr.stat;
  break;

case CMPP.CMPPE_ACTIVE_RESP:
  ret = "---------active resp-----------: STAT " + cr.stat;
  break;

default:
  ret = "---------Error packet-----------";
  break;

      }

    }
    catch (Exception e) {
      System.out.println(e.getMessage());
      e.printStackTrace();
      System.out.println("have a exception");
      try {
System.in.read();
      }
      catch (Exception e1) {}

    }
    return ret;
  }
  /**
   *
   * @param str
   * @return
   */
  private static byte[] StrToByte(String str){
byte[] tmp = str.getBytes();
byte[] ret = new byte[tmp.length+1];
for (int i=0;i<tmp.length;i++)
{
  ret[i]=tmp[i];
}
ret[tmp.length]=0x0;
return ret;
  }
  /**
   *
   * @param key
   * @return
   */

  /**
   *
   * @param tel
   * @param msg
   * @return
   */
  private cmppe_submit initCMPP(String tel,byte[] short_msg){
CMPP p = new CMPP();
    cmppe_submit sub = new cmppe_submit();
    System.out.println(tel);
    byte[] icp_id = StrToByte("916307");
    //byte[] icp_id = StrToByte("106573300011");
    //byte svc_type[] = StrToByte("06006");
    byte fee_type = 1;
    byte info_fee = 0;
    byte proto_id = 1;
    byte msg_mode = (byte) 8; // :0-, 1-, 2-
    byte priority = 0;
    byte fee_utype = 0;
    byte validate[] = {0};
    byte schedule[] = {0};


    tel = this.checkTel( tel );//
    String mobile[] = tel.split(",");
    System.out.println("mobile.length:" + mobile.length);
    byte[][] dst_addr = new byte[mobile.length+1][CMPP.CMPPE_MAX_MSISDN_LEN];
    byte src_addr[] = StrToByte("106573300011");
    //byte src_addr[] = StrToByte("06006");
    for (int i=0;i<mobile.length;i++)
    {
      byte[] tmp = StrToByte(mobile[i]);
      for (int j=0;j<tmp.length;j++)
      {
dst_addr[i][j] =tmp[j];
      }
    }
    /*
    for (int i=0;i<dst_addr[0].length;i++)
    {
      src_addr[i]=dst_addr[0][i];
    }
    */
    byte du_count = (byte) mobile.length;
    System.out.println("");
    System.out.println(du_count);
    byte data_coding = 0x8;
   // byte[] short_msg = StrToByte(msg);
    int sm_len = short_msg.length;

    System.out.println("short message length:" + sm_len);
    System.out.println("msg:" + new String(short_msg));
    try{
    sub.set_icpid(icp_id);
    //sub.set_svctype(svc_type);
    sub.set_feetype(fee_type);
    sub.set_infofee(info_fee);
    sub.set_protoid(proto_id);
    sub.set_msgmode(msg_mode);
    sub.set_priority(priority);
    sub.set_validate(validate);
    sub.set_schedule(schedule);
    sub.set_feeutype(fee_utype);
    //sub.set_feeuser(fee_user);
    sub.set_srcaddr(src_addr);
    sub.set_dstaddr(dst_addr);
    sub.set_ducount(du_count);
    sub.set_msg(data_coding, sm_len, short_msg);
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    return sub;
  }



//
String checkTel(String tel){

  String mobeil="";

  boolean flag= false;

  int length=tel.length();

  for(int i=0; i<length; i++){
    int v=(int)tel.charAt(i);

    if(v==44 && flag){
       mobeil += tel.charAt(i);
       flag=false;
    }
    else if(v>=48 && v<=57){
       mobeil += tel.charAt(i);
       flag=true;

    }
  }

  return mobeil;

}

  /**
   *
   * @param tel
   * @param msg
   * @return
 * @throws UnsupportedEncodingException 
   */
  static String sendSms(String tel,String msg) throws UnsupportedEncodingException {
    int user_seq;
    CMPP p = new CMPP();
    byte[] messageUCS2; 

    messageUCS2= msg.getBytes("UnicodeBigUnmarked");
   // messageUCS2 = StrToByte(msg);
    

    int msgLen  = messageUCS2.length;
    int messageUCS2Len = 140;
    int msgPart = msgLen/(messageUCS2Len-6) +1;//长短信分为多少条发送
    
    
    shortMsg sms = new shortMsg();
  /*  for (int i=0; i<msgArray.length; i++){
     
     byte [] head = { 5, 0, 3, 0, (byte) msgPart, (byte) (i+1) };
    // System.arraycopy(head, 0, msgArray[i], 0, 6);
     byte msgContent[] = new byte[msgArray[i].length()+6];
     System.arraycopy(head, 0, msgContent, 0, 6);
     System.arraycopy(StrToByte(msgArray[i]), 0, msgContent, 6, msgArray[i].length());
    cmppe_submit sub =sms.initCMPP(tel,msgContent);
    
    for (int test_count = 0; test_count < 1; test_count++) {
//user_seq = p.cmpp_submit(con, sub);
//System.out.println("Submited SM Seq = " + user_seq);

//if (msg_mode == 1) { //
//  ret = sms.readPa(con);
//}
    }
    }*/
    cmppe_submit_result res = new cmppe_submit_result();
    cmppe_cancel can = new cmppe_cancel();
    cmppe_result cr = new cmppe_result();
    cmppe_deliver_result rs = new cmppe_deliver_result();
    Date date = new Date();
    int s_min = (int) (date.getTime() / 1000);
    String ret="";
    try {
      conn_desc con = new conn_desc();
      String host="10.87.247.144";
      //String host ="127.0.0.1";
      int port = Integer.parseInt("7890");
      p.cmpp_connect_to_ismg(host, port, con);
      System.out.println("Connected ISMG");
      p.cmpp_login(con, "916307", "EOMS9876", 2, 0x12, s_min);
      
      
     // System.out.println(sms.readPa(con));
      for (int i=0; i<msgPart; i++){
       byte[] head=new byte[6]; 
       head[0]=0x05; 
       head[1]=0x00; 
       head[2]=0x03; 
       head[3]=0x0A; 
       head[4]=(byte)msgPart; 
       head[5]=(byte) (i+1);//默认为第一条 

       
       
      // System.arraycopy(head, 0, msgArray[i], 0, 6);
      /* byte[]messageUCS2;
       messageUCS2 = message.getBytes("UnicodeBigUnmarked");*/

       
       
       /*byte msgContent[] = new byte[StrToByte(msgArray[i]).length+6];
       System.arraycopy(StrToByte(msgArray[i]), 0, msgContent, 6, StrToByte(msgArray[i]).length);
       System.arraycopy(head, 0, msgContent, 0, 6);
       
       System.arraycopy(StrToByte(msgArray[i]), 0, msgContent, 6, StrToByte(msgArray[i]).length);*/
       
       
       byte[] msgContent; 
       if(i!=msgPart-1){//不为最后一条 
       msgContent=byteAdd(head, messageUCS2, i*(messageUCS2Len-6), (i+1)*(messageUCS2Len-6)); 
       }else{ 
       msgContent=byteAdd(head, messageUCS2, i*(messageUCS2Len-6), msgLen); 
       } 
       String s = new String(msgContent);
       
       System.out.println("正在发送第"+head[5]+"条长短信");  
      cmppe_submit sub =sms.initCMPP(tel,msgContent);
      
      for (int test_count = 0; test_count < 1; test_count++) {
user_seq = p.cmpp_submit(con, sub);
System.out.println("Submited SM Seq = " + user_seq);

//if (msg_mode == 1) { //
  ret = sms.readPa(con);
//}
      }
      
      }
      p.cmpp_active_test(con);
      System.out.println(sms.readPa(con));

      p.cmpp_logout(con);
      System.out.println(sms.readPa(con));

      //System.in.read();

    }
    catch (Exception e) {

      System.out.println(e.getMessage());
      e.printStackTrace();
      System.out.println("have a exception");

    }

    if (ret.equals("0")){
      return "OK";
    }
    else
    {
      return "failed";
    }

  }
  public static void main(String[] arg) throws UnsupportedEncodingException{
System.out.println("send a shortmsg to "+arg[0]+" : "+arg[1]);
sendSms(arg[0],arg[1]);
}
  
  public static  byte[] byteAdd(byte[] src,byte[] add,int start,int end){ 
  byte[] dst=new byte[src.length+end-start]; 
  for(int i=0;i<src.length;i++){ 
  dst[i]=src[i]; 
  } 
  for(int i=0;i<end-start;i++){ 
  dst[src.length+i]=add[start+i]; 
  } 
  return dst; 
  } 

  

  
}
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,