CMPP2.0-based socket client (Java)

Source: Internet
Author: User

SMS Verification Code, SMS notifications and other functions can be achieved through this socket client.

Here are some things to note:

1, the server side is read by byte, so the client sends the bytes in the packet of the fields in strict accordance with the order of the CMPP protocol, and each field must have.

2. When the actual length of each field is insufficient, use 0 to complement the number.

3, when using Bytebuffer, pay attention to the difference between putint () and put ()


I personally practice the template as follows, for your reference to study:

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

CMPP2.0-based socket client (Java)

Related Article

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.