package sms3_0;
/*
用途:CMPP消息结构体的头结构
*/
public class _CMPP_HEADER
implements java.io.Serializable {
private int Total_Length; //消息总长度(含消息头及消息体)
private int Command_Id; //命令或响应类型
private int Sequence_Id = 1; //消息流水号,顺序累加,步长为1,循环使用(一对请求和应答消息的流水号必须相同)
public _CMPP_HEADER() {
}
//产生一个新的消息流水号
protected void setSequence_Id() {
this.Sequence_Id++;
}
//设置命令或响应类型
protected void setCommand_Id(int mCommand_Id) {
this.Command_Id = mCommand_Id;
}
//读取消息总长度(含消息头及消息体)
protected int getTotal_Length() {
return this.Total_Length;
}
//设置消息总长度
protected void setTotal_Length(int mTotal_Length) {
this.Total_Length = mTotal_Length;
}
}
/////////////////////////////////////////////////////////
package sms3_0;
/*
用途:是SP向ISMG注册作为一个合法SP身份的CMPP_CONNECT消息结构
*/
import sms3_0._CMPP_HEADER;
import sms3_0._CMPP_MD5;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Date;
public class _CMPP_CONNECT extends _CMPP_HEADER
implements java.io.Serializable {
//源地址,此处为SP_Id,即SP的企业代码
private byte[] Source_Addr;
//AuthenticatorSource =MD5(Source_Addr+9 字节的0 +shared secret+timestamp)
private byte[] AuthenticatorSource;
//双方协商的版本号(高位4bit表示主版本号,低位4bit表示次版本号),对于3.0的版本,高4bit为3,低4位为0)
private byte Version;
//时间戳的明文,由客户端产生,格式为MMDDHHMMSS,即月日时分秒,10位数字的整型,右对齐
private int Timestamp;
//SP的shared secret值
private String Shared_Secret;
public _CMPP_CONNECT() {
Source_Addr=new byte[6];
for (int i=0; i<Source_Addr.length;i++) {
Source_Addr[i]=0;
}
AuthenticatorSource=new byte[16];
for (int i=0; i<AuthenticatorSource.length;i++) {
AuthenticatorSource[i]=0;
}
super.setTotal_Length(39);
super.setCommand_Id(0x00000001);
this.setVersion();
}
//获取当前时间和日期的时音
private String getNowDate() {
Date nowDate = new Date();
DateFormat DateFmt = new SimpleDateFormat("MMddHHmmss");
String sTimestamp = DateFmt.format(nowDate);
return sTimestamp;
}
//设定时间戳的明文
public void setTimestamp() {
String sTimestamp = this.getNowDate();
this.Timestamp = Integer.parseInt(sTimestamp);
}
public void setAuthenticatorSource() {
String sTimestamp = this.getNowDate();
String sAuth = this.Source_Addr+ "000000000" + this.Shared_Secret +
sTimestamp;
_CMPP_MD5 md5 = new _CMPP_MD5();
String md5Auth = md5.getMD5String(sAuth);
byte b[] = md5Auth.getBytes();
for (int i = 0; i < 16; i++) {
if (i < b.length) {
this.AuthenticatorSource[i] = b[i];
}
else {
this.AuthenticatorSource[i] = 0;
}
}
}
//设定版本号
private void setVersion() {
this.Version = 0x30;
}
//设定SP的企业代码
public void setSource_Addr(String mSource_Addr) {
byte[] b = mSource_Addr.getBytes();
for (int i = 0; i < 6; i++) {
if (i < b.length) {
this.Source_Addr[i] = b[i];
}
else {
this.Source_Addr[i] = 0;
}
}
}
//设定SP的shared secret
public void setShared_Secret(String mSecret) {
this.Shared_Secret = mSecret;
}
}
//////////////////////////////////////////
package sms3_0;
import sms3_0._CMPP_CONNECT;
import java.net.Socket;
import java.io.*;
public class Test_CMPP {
public Test_CMPP() {
}
public static void main(String[] args) {
try {
_CMPP_CONNECT cmpp_connect=new _CMPP_CONNECT();
cmpp_connect.setSource_Addr("901236");
cmpp_connect.setShared_Secret("1236");
cmpp_connect.setTimestamp();
cmpp_connect.setAuthenticatorSource();
cmpp_connect.setSequence_Id();
Socket CMPP_Socket = new Socket("127.0.0.1", 7890);
ObjectOutputStream OutSend = new ObjectOutputStream(CMPP_Socket.getOutputStream());
OutSend.writeObject(cmpp_connect);
InputStream a=CMPP_Socket.getInputStream();
}
catch (IOException e) {
System.out.println("Socket连接出错了!错误为描述为" +
e.getMessage());
System.out.println("Socket未打开");
}
}
}