About using JAVA to develop text messages

Source: Internet
Author: User

 



First, you need to select a mobile or Unicom SMS Server as your SMS sending interface. this is the most critical step and the bottom layer. Of course, it requires much money. no way! Reality ....

Then you are doing technology on the server connecting to the interface. it is difficult to use a java socket to listen to the client SOCKET, for example, you can accept 3000 sockets at the same time every second ......, client authentication is required. data Stream Transmission encryption ......

The last step is to process the client's sending SOCKET. In fact, they all transmit Byte...

Public class Client
{
// Constructor
Public Client ()
{
ConnectStatus = false;
LoginStatus = false;
Default_ip = "";
Default_port = 0;
Default_user = "";
Default_password = "";
Connection = null;
Input = null;
Out = null;
Binput = null;
}
// Connection Function

Public boolean connect (String ip, int port)
{
Boolean result = false;
If (ip = null | ip. indexOf (".") <1)
Ip = default_ip;
If (port <1)
Port = default_port;
Try
{
Connection = new Socket (ip, port );
Input = connection. getInputStream ();
Binput = new BufferedInputStream (input );
Out = connection. getOutputStream ();
Result = true;
ConnectStatus = true;
Default_ip = ip;
Default_port = port;
}
Catch (Exception e)
{
Result = false;
}
Return result;
}

// Logon Function

Public int login (String userName, String password)
{
Int loginRes =-1;
If (! ConnectStatus)
Return 1;
If (userName = null | userName. trim (). equals (""))
Return 24;
Byte loginData [] = new byte [43];
SetIntData (43, loginData, 0 );
SetIntData (1, loginData, 4 );
SetIntData (1, loginData, 8 );
UserName. getBytes (0, userName. length (), loginData, 12 );
Password. getBytes (0, password. length (), loginData, 22 );
LoginData [38] = 1;
SetTime (new Date (). getTime ()/(long) 1000, loginData, 39 );
Try
{
Out. write (loginData );
Out. flush ();
Byte rec [] = new byte [30];
Char ret =;
Int I = 0;
Do
{
If (I >= 400)
Break;
If (binput. available ()> 0)
{
Input. read (rec, 0, 30 );
Ret = (char) rec [12];
Break;
}
Thread. sleep (50L );
I ++;
} While (true );
If (ret = T)
{
LoginStatus = true;
Default_user = userName;
Default_password = password;
Int k = 0;
Return k;
}
LoginRes = 1;
}
Catch (Exception e)
{
Int j = 1;
Return j;
}
Return loginRes;
}

// Sending Function

Public int submit (String servicer_id, int msg_type, int status_report, String src_addr, String

Dest_addr, String schedule, String expire,
Int msg_fmt, byte msg_content [], String fee_type, String fee_code, String fee_mobile,

String fee_by, int pid)
{
Int subRes =-1;
If (! ConnectStatus |! LoginStatus)
Return 1;
Byte submitData [] = new byte [264];
SetIntData (264, submitData, 0 );
SetIntData (4, submitData, 4 );
SetIntData (1, submitData, 8 );
Servicer_id.getBytes (0, servicer_id.length (), submitData, 12 );
SubmitData [22] = (byte) msg_type;
SubmitData [23] = (byte) status_report;
If (src_addr = null | src_addr.trim (). length () <1)
Return 14;
Src_addr.getBytes (0, src_addr.length (), submitData, 24 );
If (dest_addr = null | dest_addr.trim (). length () <1)
Return 15;
Dest_addr.getBytes (0, dest_addr.length (), submitData, 45 );
If (schedule! = Null & schedule. trim (). length ()> 1 &&! SetTime (schedule, submitData,

66 ))
Return 16;
If (expire! = Null & expire. trim (). length ()> 1 &&! SetTime (expire, submitData, 70 ))
Return 17;
SubmitData [74] = (byte) msg_fmt;
SubmitData [75] = (byte) msg_content.length;
System. arraycopy (msg_content, 0, submitData, 76, msg_content.length );
Fee_type.getBytes (0, fee_type.length (), submitData, 236 );
Fee_code.getBytes (0, fee_code.length (), submitData, 244 );
If (fee_mobile! = Null & fee_mobile.trim (). length ()> 11)
Fee_mobile = fee_mobile.substring (2 );
Fee_mobile.getBytes (0, fee_mobile.length (), submitData, 250 );
Fee_by.getBytes (0, 2, submitData, 261 );
If (pid <0 | pid> 256)
Pid = 0;
SubmitData [263] = (byte) pid;
Boolean reSend = false;
Try
{
Out. write (submitData );
Out. flush ();
}
Catch (Exception e)
{
ReSend = true;
}
If (reSend)
{
Int sendTimes = 0;
Do
{
If (sendTimes> = 2)
Break;
Try
{
Connect (default_ip, default_port );
Login (default_user, default_password );
Out. write (submitData );
Out. flush ();
ReSend = false;
Break;
}
Catch (Exception exception)
{
SendTimes ++;
}
} While (true );
}
If (reSend)
{
ConnectStatus = false;
LoginStatus = false;
Return 1;
}
Try
{
Byte sub [] = new byte [17];
Char sRet =;
Int I = 0;
Do
{
If (I >= 1500)
Break;
If (binput. available ()> 0)
{
Input. read (sub, 0, 17 );
SRet = (char) sub [16];
Break;
}
Thread. sleep (10L );
I ++;
} While (true );
If (sRet = T)
SubRes = 0;
Else
SubRes = 11;
}
Catch (Exception e)
{
SubRes = 1;
}
Return subRes;
}


Of course we need to judge different errors and correct sending based on different parameters. So much talk about it. If you have any questions, please contact me.

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.