CMPP2.0-based Socket Client (Java)

Source: Internet
Author: User

CMPP2.0-based Socket Client (Java)

SMS verification code, SMS notification, and other functions can be implemented through this Socket Client.

Note:

1. The server reads data in bytes in sequence. Therefore, fields in the byte packet sent by the client must strictly follow the order in the CMPP protocol, and each field must have one.

2. When the actual length of each field is insufficient, fill in with 0; fill in with space after the phone number

3. When using ByteBuffer, pay attention to the difference between putInt () and put ().


The templates I have personally practiced are as follows for your reference:

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 spids = "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,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:" + );
}
}

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.