简体   繁体   English

SMPP接收器Java客户端

[英]SMPP Reciever Java Client

I've build a java program to listen to SMPP server and captures the SMS sent to that server it works fine but after certain intervals I'm getting different types of errors as below. 我已经构建了一个Java程序来侦听SMPP服务器,并捕获发送到该服务器的SMS,它工作正常,但是在一定的间隔后,我遇到了以下不同类型的错误。

Also can anyone tell me how to make changes that this java code captures only messages sent yo a particular SC 也有人可以告诉我如何进行更改,以便此Java代码仅捕获从特定SC发送的消息

ERROR: com.logica.smpp.pdu.EnquireLink cannot be cast to com.logica.smpp.pdu.DeliverSM ERROR:com.logica.smpp.pdu.Unbind cannot be cast to com.logica.smpp.pdu.DeliverSM 错误:com.logica.smpp.pdu.EnquireLink无法转换为com.logica.smpp.pdu.DeliverSM错误:com.logica.smpp.pdu.Unbind无法转换为com.logica.smpp.pdu.DeliverSM

And my code is as below: 我的代码如下:

import com.logica.smpp.Data;
import com.logica.smpp.Session;
import com.logica.smpp.TCPIPConnection;
import com.logica.smpp.pdu.BindReceiver;
import com.logica.smpp.pdu.BindRequest;
import com.logica.smpp.pdu.BindResponse;
import com.logica.smpp.pdu.DeliverSM;
import com.logica.smpp.pdu.PDU;

public class SimpleSMSReceiver {
/** * Parameters used for connecting to SMSC (or SMPPSim)*/
    private Session session = null;
    private String ipAddress = "localhost";
    private String systemId = "smppclient1";
    private String password = "password";
    private int port = 2775;

/** * @param args */
    public static void main(String[] args) {
        System.out.println("Sms receiver starts");
        SimpleSMSReceiver objSimpleSMSReceiver = new SimpleSMSReceiver();
        objSimpleSMSReceiver.bindToSmsc();
        while(true) {
            objSimpleSMSReceiver.receiveSms();
        }
    }
    private void bindToSmsc() {
        try {
            // setup connection
            TCPIPConnection connection = new TCPIPConnection(ipAddress, port);
            connection.setReceiveTimeout(20 * 1000);
            session = new Session(connection);
            // set request parameters
            BindRequest request = new BindReceiver();
            request.setSystemId(systemId);
            request.setPassword(password);
            // send request to bind
            BindResponse response = session.bind(request);
            if (response.getCommandStatus() == Data.ESME_ROK) {
                System.out.println("Sms receiver is connected to SMPPSim.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void receiveSms() {
        try {
            PDU pdu = session.receive(1500);
            if (pdu != null) {
                DeliverSM sms = (DeliverSM) pdu;
                if ((int)sms.getDataCoding() == 0 ) {
                    //message content is English
                    System.out.println("***** New Message Received *****");
                    System.out.println("From: " + sms.getSourceAddr().getAddress());
                    System.out.println("To: " + sms.getDestAddr().getAddress());
                    System.out.println("Content: " + sms.getShortMessage());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

PDU may not be always DeliverSM. PDU可能不总是DeliverSM。

Try this: 尝试这个:

PDU pdu = session.receive(1500);
if ((pdu != null) && (pdu instanceof DeliverSM)) {
    ...
    DeliverSM sms = (DeliverSM) pdu;

As you see, SMSC is sending Heartbeats (EnquireLink) so you must positive answer to those as well. 如您所见,SMSC正在发送心跳(EnquireLink),因此您也必须对它们进行肯定答复。 In case you dont acknowledge heartbeats, server will think connection is deas and will close it (Unbind). 万一您不确认心跳,服务器将认为连接已断开并关闭连接(解除绑定)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM