开发包:中兴的开发包
流程:手机发短信给网关-》网关-》我的系统,回复的时候:我的系统-》短信网关
问题:系统接收到短信网关的消息,怎样给短信网关回复我已经收到消息
代码:
import java.io.IOException;
import java.net.UnknownHostException;import org.apache.log4j.Logger;import com.ysb.gw.comm.ConfigUtil;
import com.zte.smgw.api.sgip.server.SGIPServer;
import com.zte.smgw.api.sgip.server.SGIPServerInitInfo;/**
 * 短信网关本系统服务端,供短信网关调用,短信下行服务端
 *
 */
public class Server {
private static Logger logger = Logger.getLogger(Server.class); /**
 * 公共变量,客户端初始化配置信息
 */
private static SGIPServerInitInfo info = null; /**
 * 构造SGIPClientInfo
 * 
 * @return SGIPClientInfo实例
 */
private SGIPServerInitInfo getSGIPServerInitInfo() {
if (info != null) {
return info;
} else {
logger.info("第一次调用,初始化本系统作为服务端的配置信息");
logger.info("SIC SMS server info..................");
logger.info("SIC_SMSSERVER_IP="+ConfigUtil.getValueByKey("SIC_SMSSERVER_IP"));
logger.info("SIC_SMSSERVER_PORT="+ConfigUtil.getValueByKey("SIC_SMSSERVER_PORT"));
logger.info("SIC_SMSSERVER_USERNAME="+ConfigUtil.getValueByKey("SIC_SMSSERVER_USERNAME"));
logger.info("SIC_SMSSERVER_PASS="+ConfigUtil.getValueByKey("SIC_SMSSERVER_PASS"));
// 判断是否有值
if (ConfigUtil.getValueByKey("SIC_SMSSERVER_IP") == null
|| ConfigUtil.getValueByKey("SIC_SMSSERVER_PORT") == null
|| ConfigUtil.getValueByKey("SIC_SMSSERVER_USERNAME") == null
|| ConfigUtil.getValueByKey("SIC_SMSSERVER_PASS") == null) {
logger.info("服务端配置信息有误,请确认!");
return null;
}
// 设置配置参数
info = new SGIPServerInitInfo(); // 服务端IP地址
info.IP = ConfigUtil.getValueByKey("SIC_SMSSERVER_IP"); // 服务端端口
info.port = Integer.parseInt(ConfigUtil
.getValueByKey("SIC_SMSSERVER_PORT")); // 用户名
info.userName = ConfigUtil.getValueByKey("SIC_SMSSERVER_USERNAME"); // 密码
info.passWord = ConfigUtil.getValueByKey("SIC_SMSSERVER_PASS");
logger.info("第一次调用,初始化本系统作为服务端的配置信息结束.............");
return info;
}
} public void startServer() throws UnknownHostException, IOException, ClassNotFoundException {
this.getSGIPServerInitInfo();
if(info == null){
logger.info("本系统作为服务端的配置信息有误,启动服务失败!.............");
return;
}
SGIPServer server = SGIPServer.getInstance();
server.init(info);
        server.start();
        logger.info("本系统作为服务端成功启动服务!..............");
}

public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException{
Server server = new Server();
server.startServer();
}
}
下面的短信是监听接收短信网关来的消息
import org.apache.log4j.Logger;import com.zte.smgw.api.sgip.message.SGIPDeliver;
import com.zte.smgw.api.sgip.message.SGIPDeliverResp;
import com.zte.smgw.api.sgip.message.SGIPReport;
import com.zte.smgw.api.sgip.message.body.SGIPCommonRespBody;
import com.zte.smgw.api.sgip.server.SGIPRecieveMsg;
import com.zte.smgw.api.sgip.server.SGIPServer;/**
 * 监听短信网关客户端请求
 *
 */
public class ReceiveThread implements Runnable {
private static Logger logger = Logger.getLogger(ReceiveThread.class);
    private Thread receiveThread;    public ReceiveThread() {
        receiveThread = new Thread(this);
        receiveThread.start();
    }    public void run() {
     logger.info("开始监听客户端请求.....");
        SGIPServer server = SGIPServer.getInstance();
        while (true) {
            if (server.MessageLength() > 0) {
             logger.info("客户端请求.....");
                SGIPRecieveMsg msg = server.ReceiveMessage();
                logger.info("客户端请求类型,messageType="+msg.messageType);
                if (msg.messageType == 1) {
                    // deliver 
                    SGIPDeliver deliver = (SGIPDeliver) msg.obj;
                    logger.info("messageType==1 and message body="+deliver.getBody());
                    String resMsg = "";
                    if(deliver.getBody().getMessageCoding() == 8){
                     try {
                    
                     logger.info("messageContentLength="+deliver.getBody().getLength());
                     //编码处理
resMsg = new String(deliver.getBody().getMessageContent(),"UnicodeBigUned");
 logger.info("this msg length="+resMsg.length());
 logger.info("messageContent="+resMsg);
} catch (Exception e) {
e.printStackTrace();
}
                    }else{
                     logger.info("messageContentLength="+deliver.getBody().getLength());
                      resMsg = new String(deliver.getBody().getMessageContent());
                      logger.info("messageContent="+resMsg);
                      logger.info("this msg length="+resMsg.length());
                    }
                    //回复给短信网关说明接受成功,否则网关会重复发送3次
                    SGIPDeliverResp resp = new SGIPDeliverResp();
                    SGIPCommonRespBody  resBody = new SGIPCommonRespBody();
                    resBody.setResult(0);
                    resp.setBody(resBody);
                    //怎么进行回复?

                } else if (msg.messageType == 2) {
                    // report 
                    SGIPReport report = (SGIPReport) msg.obj;
                    logger.info("messageType==2 and message body="+report.getBody());
                    //
                    if(report.getBody().getState()==0){
                     //发送短信成功
                    }else{
                     //发送短信失败
                    }
                }
            }
        }
    }
}

解决方案 »

  1.   

    应该会有一个类似sumbit_sm的接口方法来给网关回响应的。
      

  2.   

    现在给手机发送短信都没有,接收手机发送的短信也没问题,就是接受到手机短信,响应给网关接受成功的时候,找不到响应的方法,用的中兴的开发包,在联通的SGIP开发包上面做了二次封装,中兴又给不出具体的接口协议
      

  3.   

    SMPP、CMPP、SGIP协议文档网上都可以下的到的。
      

  4.   

    SGIP协议手中已经得到(其中这样描述)Deliver_Resp应答的语法字段 长度(字节) 类型    说明
    Result 1         Integer    Deliver命令是否成功接收。0:接收成功
    其它:错误码
    Reserve 8 Text 保留,扩展用到底该怎么应答没有说明
      

  5.   

    楼主,我最近也在做联通的短信开发, 可是在测试它提供的demo的时候就通不过,老是出现错误:DEBUG [Thread-1] com.zte.smgw.socket.client.ClientSendThread     - message sending
     ERROR [Thread-3] com.zte.smgw.socket.client.SocketClientLink     - SockectClientLink read message error,key:sun.nio.ch.SelectionKeyImpl@e80a59
     java.io.IOException: 远程主机强迫关闭了一个现有的连接。
    at sun.nio.ch.SocketDispatcher.read0(Native Method)
    at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:25)
    at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:233)
    at sun.nio.ch.IOUtil.read(IOUtil.java:206)
    at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:236)
    at com.zte.smgw.socket.client.SocketClientLink.read(SocketClientLink.java:141)
    at com.zte.smgw.socket.client.SocketClientLink.run(SocketClientLink.java:125)
    at java.lang.Thread.run(Thread.java:619)
    INFO  [main] com.zte.smgw.socket.client.ClientSendThread     - 消息数:0
    能不能帮我我看一下,我到底是哪里配置不对呢
      submit.getBody().setSPNumber("106550251225098564");
                submit.getBody().setChargeNumber("13196603534");
                submit.getBody().setUserCount(1);
                submit.getBody().setUserNumber("15895435391");
                submit.getBody().setCorpId("98564");
                submit.getBody().setFeeType(2);
                submit.getBody().setFeeValue("4");
                submit.getBody().setGivenValue("987");
                submit.getBody().setAgentFlag(0);
                submit.getBody().setMorelatetoMTFlag(0);
                submit.getBody().setPriority(0);
                submit.getBody().setExpireTime("090621010101");
                submit.getBody().setScheduleTime("090621010101");
                submit.getBody().setReportFlag(0);
                submit.getBody().setTP_pid(1);
                submit.getBody().setTP_udhi(1);
                submit.getBody().setMessageCoding(15);
                submit.getBody().setMessageType(0);
                submit.getBody().setMessageContent("zhjt2222est".getBytes());            submit.getBody().setReserve("dd");我都弄了好几天了,都没配置好,郁闷死了网关地址端口都是对的,已经配好他们提供的短信平台,就是程序通不过(借地方发牢骚,联通技术服务极其差,网关地址都能给搞错,我们这面实在连平台都配不进去了,追问了好几天,才找到对方的人,说是东西少发了,mmd)
              楼主救命。
      

  6.   

    我这几天也在用中兴开发包连联通的短信平台
    DEBUG [Thread-1] com.zte.smgw.socket.client.ClientSendThread     - message sending
     ERROR [Thread-3] com.zte.smgw.socket.client.SocketClientLink     - SockectClientLink read message error,key:sun.nio.ch.SelectionKeyImpl@e80a59
     java.io.IOException: 远程主机强迫关闭了一个现有的连接。
        at sun.nio.ch.SocketDispatcher.read0(Native Method)
        at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:25)
        at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:233)
        at sun.nio.ch.IOUtil.read(IOUtil.java:206)
        at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:236)
        at com.zte.smgw.socket.client.SocketClientLink.read(SocketClientLink.java:141)
        at com.zte.smgw.socket.client.SocketClientLink.run(SocketClientLink.java:125)
        at java.lang.Thread.run(Thread.java:619)
    INFO  [main] com.zte.smgw.socket.client.ClientSendThread     - 消息数:0
    报这种错误应该是参数不对 按照下面的参数试一下 不行的话你要找联通问一下参数了
      submit.getBody().setSPNumber("106550251225098564");
                submit.getBody().setChargeNumber("8613196603534");//
                submit.getBody().setUserCount(1);
                submit.getBody().setUserNumber("15895435391");//
                submit.getBody().setCorpId("98564");
                submit.getBody().setFeeType(1);
                submit.getBody().setFeeValue("0");
                submit.getBody().setGivenValue("0");
                submit.getBody().setAgentFlag(0);
                submit.getBody().setMorelatetoMTFlag(0);
                submit.getBody().setPriority(0);
                submit.getBody().setExpireTime("");
                submit.getBody().setScheduleTime("");
                submit.getBody().setReportFlag(0);
                submit.getBody().setTP_pid(0);//1
                submit.getBody().setTP_udhi(0);//1
                submit.getBody().setMessageCoding(15);
                submit.getBody().setMessageType(0);
              submit.getBody().setMessageContent("你好".getBytes());            submit.getBody().setReserve("dd");
    我也也有一个疑问楼主使用完了以后如何关闭这个客户端和服务端
      

  7.   

    第一:是长连接,不用关闭
    第二:远程主机强迫关闭了一个现有的连接,可能的原因,短信网关需要绑定你客户端服务器的IP地址,请确认是否有绑定,绑定的是否是你现在测试的这台服务器的IP地址
    第三:请确认分配给你接入的用户名密码是否正确
      

  8.   

    过来几天在来测试该问题,通过debug输出日志查看,开发包已经自动进行回复响应,已不在3次重发,问题解决
      

  9.   


    我在做开发的时候,服务端监听程序出了问题,问题的现象是服务端接受短信一段时间后就报错了,日志信息如下:2011-09-05 11:21:19 [com.zte.smgw.api.sgip.server.SGIPServer]-[INFO] SGIP server is init success!
     2011-09-05 11:21:19 [test.ReceiveThread]-[INFO] run.......
     2011-09-05 11:21:19 [com.zte.smgw.socket.server.SocketServer]-[INFO] 117.135.134.135:8000 is begin to listen!
     2011-09-05 11:21:19 [com.zte.smgw.api.sgip.server.SGIPServer]-[INFO] SGIP server is start success!
     2011-09-05 11:50:06 [com.zte.smgw.socket.server.SocketServer]-[INFO] /220.196.52.117:43115 connect success!
     2011-09-05 11:50:06 [com.zte.smgw.api.sgip.server.SGIPReqHandler]-[ERROR] Bind error:username or password is not correct. SequenceNo:102103 905120145 680417317
     2011-09-05 11:50:06 [com.zte.smgw.socket.server.SocketServer]-[ERROR] /220.196.52.117:43115
     java.io.IOException: Connection reset by peer
             at sun.nio.ch.FileDispatcher.read0(Native Method)
             at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:21)
             at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:233)
             at sun.nio.ch.IOUtil.read(IOUtil.java:206)
             at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:236)
             at com.zte.smgw.socket.server.SocketServer.read(SocketServer.java:170)
             at com.zte.smgw.socket.server.SocketServer.run(SocketServer.java:137)
             at java.lang.Thread.run(Thread.java:619)
    2011-09-05 11:55:10 [com.zte.smgw.socket.server.SocketServer]-[INFO] /220.196.52.117:44356 connect success!
     2011-09-05 11:55:10 [com.zte.smgw.api.sgip.server.SGIPReqHandler]-[ERROR] Bind error:username or password is not correct. SequenceNo:102103 905120649 680470055
     2011-09-05 11:55:10 [com.zte.smgw.socket.server.SocketServer]-[ERROR] /220.196.52.117:44356
     java.io.IOException: Connection reset by peer
             at sun.nio.ch.FileDispatcher.read0(Native Method)
             at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:21)
             at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:233)
             at sun.nio.ch.IOUtil.read(IOUtil.java:206)
             at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:236)
             at com.zte.smgw.socket.server.SocketServer.read(SocketServer.java:170)
             at com.zte.smgw.socket.server.SocketServer.run(SocketServer.java:137)
             at java.lang.Thread.run(Thread.java:619)
    请问服务端的监听程序该怎么去判断当前的连接是否有效呢?
      

  10.   

    核心思想:多线程的生产者与消费者模式。 
    实现思路: 
    1、开启一个线程,从待发表中查询出待发短信,包转好后放入待发队列。 
    2、开启十个发送线程,从待发队列中取出数据,发送给移动网关。发送后,发送后的数据进入历史表,同时把这条数据从待发表中移除。
     
    欢迎加入群125797984 讨论移动短信网关和联通短信网关 
    CMPP协议 
    SGIP协议 
      

  11.   

    楼主~我也是用的中兴开发包!
    但是跑起来就是莫名其妙下报错!
    求楼主的源码一看~~
      邮箱[email protected]