基于CMPP2.0的Socket客户端(Java) - 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="000000000001069034531".getBytes();
DestUsr_tl=1;
Dest_terminal_Id="000000000018511790624".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
    评论
《中国移动通信CMPP2.0短消息网关开发接口库》 本接口是用VC6.0遵循中国移动通信标准CMPP2.0协议开发的短消息网关接口,目的是简化SP接入移动短消息网关的复杂度。 本接口对网络通信和CMPP2.0协议进行了高度封,连接过所有类型的移动网关,暂时不支持CMPPE(东软格式,但东软也支持标准CMPP). 本接口还提供了网关的部分功能接口,可以很方便地用来开发模拟网关),接口库就提供了一个功能完整的模拟网关源码!接口包含两个示例工程,一个是客户端,通过CMPP2.0连接网关,演示了登录、MT、MO的全过程;一个就是模拟网关,可以接收用户的登录,接收MT请求,同时还可以主动批量下发MO消息。 更新历史: 2003-08-08 V1.3 1. 增加了对cmpp_recv函数的返回值CMPP_RECVTIMEOUT的说明。 2. 修改了cmpp_login_value_set()函数,去掉了一个参数。 2003-07-23 V1.22 1.增加了两个函数(cmpp_submit_sm_set_msgsrc和cmpp_submit_sm_set_srctermid),可以分别用来设置Src_Id和Msg_src。 根据移动的要求(升级后的网关),sp下发消息时,Src_Term_Id和Msg_src必须为该连接的对端的SP的服务代码和企业代码 (见《关于SP下发数据格式的规范》). 2003-07-08 V1.2 1.修正了cmpp_login_value_set函数的一个bug。 2003-06-07 V1.1 1.增加了网关接收部分的接口,同时新增了详细使用手册和模拟网关的源码,并且在真实环境中测试通过! 2003-04-25 V1.0 1.0发布。 作者: princetoad Email:[email protected] Homepage:http://gmm.nease.net 1.3版下载地址:http://gmm.nease.net/CMPP.rar 1.22版下载地址:http://skybird.9s9s.com/upload/upfile/2003723956540.rar 1.21版下载地址:http://www.csdn.net/cnshare/soft/17/17025.shtm 1.2版下载地址:http://skybird.9s9s.com/upload/upfile/2003781329230.rar 1.1版下载地址:http://www.vckbase.com/code/listcode.asp?mclsid=9&sclsid=917 本接口库有偿提供源码,请到中国共享软件注册中心注册或联系作者,[email protected]。 注册地址:http://www.softreg.com.cn/shareware.asp?id=11530 同时提供中国联通SGIP1.2接口库,更多详情请访问作者主页! 下载:http://gmm.nease.net/SGIP.rar 注册地址:http://www.softreg.com.cn/shareware.asp?id=11517

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值