⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smssender.java

📁 使用短信猫发送短信的程序源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * 
 *
 */
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.sql.*;

public class SMSSender {

	public SMSSender() {
        STATUS_WAIT = 0;
        STATUS_NOT_SEND = 1;
        STATUS_SENT_OK = 2;
        STATUS_SENT_FAIL = 3;
        STATUS_RECEIVED = 4;
        STATUS_SENDING = 5;
        STATUS_CANCEL = 6;
        SMS_DELIVER = 0;
        SMS_SUBMIT = 1;
        SMS_STATUS_REPORT = 2;
        System.out.println("--------------------------------");
        System.out.println("J Message Pro Server");
        System.out.println("--------------------------------");
        System.out.print("Message Delivery Engine ( V" + JM_VERSION + " / ");
        System.out.println(System.getProperty("os.name") + " " + System.getProperty("os.version") + " )");
        System.out.println("");
        jm = new JMDefine(true);
        System.out.println("");
        System.out.print(JMTools.getDatetimeString() + "\tSYS:\tInitializing ... ");
        strMsgList = "";

        dataTools = new DataTools(jm.getDB_DRIVER(),jm.getDB_URL(), jm.getDB_LOGIN(), jm.getDB_PASSWD());
        if(!dataTools.DataConnect())
        {
            System.out.println("ERROR: can't connect to database server.");
            System.exit(1);
        }
        try
        {
            conn = new Socket(InetAddress.getByName(jm.getGATEWAY_IP()), jm.getGATEWAY_PORT());
            conn.setSoTimeout(jm.getTIMEOUT_SEC() * 1000);
            input = new DataInputStream(conn.getInputStream());
            output = new DataOutputStream(conn.getOutputStream());
        }
        catch(Exception exception)
        {
            System.out.println("ERROR: can't connect to IP-GSM gateway.");
            System.exit(1);
        }
        bCommError = false;
        if(initComm() != 0)
        {
            System.out.println("ERROR: can't initialize connection to IP-GSM gateway.");
            System.exit(1);
        }

        System.out.println("OK");
    
	}

    private int initComm()
    {
        if(setMsgFormat(0) != 0)
            return 1;
        InitAT initat = new InitAT();
        for(int i = 0; i < initat.commands.size(); i++)
            if(execAT((String)initat.commands.get(i)) != 0)
                return 100 + i;

        return 0;
    }

    private int setMsgFormat(int i)
    {
        byte abyte0[] = new byte[1024];
        String s2 = "";
        try
        {
            String s = "AT+CMGF=" + Integer.toString(i) + "\r";
            output.write(s.getBytes());
            output.flush();
            s2 = "";
            int j;
            while((j = input.read(abyte0, 0, 1024)) != -1) 
            {
                String s1 = new String(abyte0, 0, j);
                s2 = s2 + s1;
                if(s2.endsWith("OK\r\n") || s2.endsWith("ERROR\r\n"))
                    break;
            }
        }
        catch(Exception exception)
        {
            System.out.println(JMTools.getDatetimeString() + "\t[comm error : set msg format]");
            bCommError = true;
        }
        return s2.endsWith("OK\r\n") ? 0 : 1;
    }

    private int execAT(String s)
    {
        byte abyte0[] = new byte[1024];
        String s3 = "";
        try
        {
            String s1 = s + "\r";
            output.write(s1.getBytes());
            output.flush();
            s3 = "";
            int i;
            while((i = input.read(abyte0, 0, 1024)) != -1) 
            {
                String s2 = new String(abyte0, 0, i);
                s3 = s3 + s2;
                if(s3.endsWith("OK\r\n") || s3.endsWith("ERROR\r\n"))
                    break;
            }
        }
        catch(Exception exception)
        {
            System.out.println(JMTools.getDatetimeString() + "\t[comm error : exec at command]");
            bCommError = true;
        }
        return s3.endsWith("OK\r\n") ? 0 : 1;
    }
    
    
    private int getMsgCount()
    {
        byte abyte0[] = new byte[1024];
        String s2 = "";
        try
        {
            String s = "AT+CPMS?\r";
            output.write(s.getBytes());
            output.flush();
            s2 = "";
            int i;
            while((i = input.read(abyte0, 0, 1024)) != -1) 
            {
                String s1 = new String(abyte0, 0, i);
                s2 = s2 + s1;
                if(s2.endsWith("OK\r\n") || s2.endsWith("ERROR\r\n"))
                    break;
            }
        }
        catch(Exception exception)
        {
            System.out.println(JMTools.getDatetimeString() + "\t[comm error : get msg count]");
            bCommError = true;
        }
        if(!s2.endsWith("OK\r\n"))
        {
            return -1;
        } else
        {
            int k = s2.lastIndexOf(",");
            int j = s2.lastIndexOf(",", k - 1);
            String s3 = s2.substring(j + 1, k);
            int l = (new Integer(s3)).intValue();
            return l;
        }
    }

    private int getMsgList()
    {
        byte abyte0[] = new byte[1024];
        String s2 = "";
        try
        {
            String s = "AT+CMGL=4\r";
            output.write(s.getBytes());
            output.flush();
            s2 = "";
            int i;
            while((i = input.read(abyte0, 0, 1024)) != -1) 
            {
                String s1 = new String(abyte0, 0, i);
                s2 = s2 + s1;
                if(s2.endsWith("\r\nOK\r\n") || s2.endsWith("\r\nERROR\r\n"))
                    break;
            }
        }
        catch(Exception exception)
        {
            System.out.println(JMTools.getDatetimeString() + "\t[comm error : get msg list]");
            bCommError = true;
        }
        if(!s2.endsWith("OK\r\n"))
        {
            return 1;
        } else
        {
            strMsgList = s2;
            return 0;
        }
    }

    private int delMsg(int i)
    {
        byte abyte0[] = new byte[1024];
        String s2 = "";
        try
        {
            String s = "AT+CMGD=" + Integer.toString(i) + "\r";
            output.write(s.getBytes());
            output.flush();
            s2 = "";
            int j;
            while((j = input.read(abyte0, 0, 1024)) != -1) 
            {
                String s1 = new String(abyte0, 0, j);
                s2 = s2 + s1;
                if(s2.endsWith("OK\r\n") || s2.endsWith("ERROR\r\n"))
                    break;
            }
        }
        catch(Exception exception)
        {
            System.out.println(JMTools.getDatetimeString() + "\t[comm error : del msg]");
            bCommError = true;
        }
        return s2.endsWith("OK\r\n") ? 0 : 1;
    }
    
    private String getReplyName(String s)
    {
        int i = s.indexOf("2");
        int j = s.indexOf(" ");
        if(i != 0)
            return "";
        if(j < 0)
            return "";
        if(j > 9)
            return "";
        else
            return s.substring(1, j).toUpperCase();
    }


    private int insertMessage(String s, String s1, String s2, String s3)   //s2:收到信息时间 s1:手机号码  s3:短信内容
    {
        String s5 = getReplyName(s3);
        String s6;

        s6 = JMTools.replaceString(s3, "'", "'+char(39)+'");
        int i = 1;
        try
        {
            CallableStatement cs = dataTools.conn.prepareCall( "begin dx_savenewmsg(?,?,?); end;" ) ; 
            cs.setString(1, s1);
            cs.setString(2, s6); 
            cs.registerOutParameter(3, Types.INTEGER);            
            cs.executeUpdate();
            
            if (cs.getInt(3)==0) 
               {i =0; } 
            cs.close();
        }
        catch(SQLException sqlexception)
        {
            System.out.println(JMTools.getDatetimeString() + "\t[database error]");
            i = 1;
        }
        return i;
    }    

    private int updateStatus(int i, int j)
    {
        int k = 0;
        String s;
        if(j == STATUS_SENT_FAIL)
        {
            if(intMsgRetry < jm.getMAX_RETRY())
                s = "UPDATE DX_DXSFJL SET zt=" + Integer.toString(STATUS_SENT_FAIL) + " " + ", send_retry = send_retry+1 " + "WHERE MSGID=" + Integer.toString(i);
            else
                s = "UPDATE DX_DXSFJL SET zt=" + Integer.toString(STATUS_CANCEL) + " " + "WHERE MSGID=" + Integer.toString(i);
        } else
        if(j == STATUS_SENT_OK)
        {
           s = "UPDATE DX_DXSFJL SET zt=" + Integer.toString(j) + " " + ", cfrq = sysdate " + "WHERE MSGID=" + Integer.toString(i);
        } else
        {
            s = "UPDATE DX_DXSFJL SET zt=" + Integer.toString(j) + " " + "WHERE MSGID=" + Integer.toString(i);
        }
        try
        {
            Statement statement = dataTools.conn.createStatement();
            statement.executeUpdate(s);
            statement.close();
        }
        catch(SQLException sqlexception)
        {
            System.out.println(JMTools.getDatetimeString() + "\t[database error]");
            k = 1;
        }
        return k;
    }    

    private void processInbox()
    {
        String s = strMsgList;

        int i = s.indexOf("\r\n");
        s = s.substring(i + 2);
        do
        {
            int j = 0;
            j = s.indexOf("\r\n");
            if(j == -1)
                break;
            String s1 = s.substring(0, j);
            s = s.substring(j + 2);
            if(s1.length() == 0)
                break;
            j = s.indexOf("\r\n");
            if(j == -1)
                break;
            String s2 = s.substring(0, j);
            s = s.substring(j + 2);
            int i1 = s1.indexOf(",");
            if(s1.substring(i1 + 1, i1 + 1 + 1).equals("0") || s1.substring(i1 + 1, i1 + 1 + 1).equals("1"))
            {
                int k;
                if(s1.substring(i1 - 2, i1 - 1).equals(" ") || s1.substring(i1 - 2, i1 - 1).equals(":"))
                    k = Integer.decode(s1.substring(i1 - 1, i1)).intValue();
                else
                    k = Integer.decode(s1.substring(i1 - 2, i1)).intValue();
                String s3 = s2;
                int j1 = Integer.decode("0x" + s3.substring(0, 2)).intValue();
                s3 = s3.substring(2 + j1 * 2);
                int k1 = Integer.decode("0x" + s3.substring(0, 2)).intValue();
                int l1 = k1 & 3;
                s3 = s3.substring(2);
                if(l1 == SMS_DELIVER)
                {
                    int i2 = Integer.decode("0x" + s3.substring(0, 2)).intValue();
                    if(i2 % 2 == 1)
                        i2++;
                    s3 = s3.substring(2);
                    int k2 = Integer.decode("0x" + s3.substring(0, 2)).intValue();
                    s3 = s3.substring(2);
                    String s4 = JMTools.decodeMP(s3.substring(0, i2));
                    s3 = s3.substring(i2);
                    if(k2 == 145)
                        s4 = "+" + s4;
                    s3 = s3.substring(2);
                    int l2 = Integer.decode("0x" + s3.substring(0, 2)).intValue();
                    s3 = s3.substring(2);
                    String s5 = new String("");
                    s5 = s5 + "20" + s3.substring(1, 2) + s3.substring(0, 1) + "-";
                    s5 = s5 + s3.substring(3, 4) + s3.substring(2, 3) + "-";
                    s5 = s5 + s3.substring(5, 6) + s3.substring(4, 5) + " ";
                    s5 = s5 + s3.substring(7, 8) + s3.substring(6, 7) + ":";
                    s5 = s5 + s3.substring(9, 10) + s3.substring(8, 9) + ":";
                    s5 = s5 + s3.substring(11, 12) + s3.substring(10, 11);
                    s3 = s3.substring(14);
                    int i3 = Integer.decode("0x" + s3.substring(0, 2)).intValue();
                    s3 = s3.substring(2);
                    String s6;
                    String s7;
                    switch(l2)
                    {
                    case 0: // '\0'
                        s6 = JMTools.msgDecodeE7(s3, i3);
                        s7 = "en";

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -