• 关于用JAVA开发短信方面的知识


     
    现在流行的网络业务莫过于短信了.网易新浪等都因此而盈利,股价上涨.我凭自己的经验和公司支持,也就乘着东风来研究一下了!

    首先,你要选择一台移动或者联通的短信服务器做你们的发送短信接口.这是最关键的一步,也是最底层的,

    当然这是需要much money的,呵呵.没办法!现实了点....

    然后你就是在连接接口的服务器做技术了.用JAVA 的 SOCKET 来监听客户端的SOCKET,当然这是需要一定的

    难度,和普通是有很多的区别,例如每秒钟可以同时接受3000条SOCKET... ...,要进行客户端的认证.要进行数据

    流转输的加密... ...


    最后就是来处理客户端的发送SOCKET了,其实他们传输都是字节Byte...


    public class Client
    {
    //构造函数
    public Client()
    {
    connectStatus = false;
    loginStatus = false;
    default_ip = "";
    default_port = 0;
    default_user = "";
    default_password = "";
    connection = null;
    input = null;
    out = null;
    binput = null;
    }
    //连接函数

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

    //登录函数



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

    //发送函数



    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;



    当然要根据不同的参数来判断不同的错误和正确的发送,就谈这么多了。
  • 相关阅读:
    python import语句
    python输出中文
    python字典的格式化字符串
    emacs查找替换
    emacs书签
    [转]Ubuntu11.10 更改登录界面背景(LightDM背景)
    ubuntu samba共享文件
    emacs笔记
    .NET登录验证码实现
    WEB标准学习实践(一)
  • 原文地址:https://www.cnblogs.com/apache-x/p/5436576.html
Copyright © 2020-2023  润新知