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未打開");
?}
?
?
? }
}