SMPP 接收器 Java 客户端

SMPP Reciever Java Client

我已经构建了一个 java 程序来侦听 SMPP 服务器并捕获发送到该服务器的 SMS,它工作正常,但在一定时间间隔后,我收到如下不同类型的错误。

还有谁能告诉我如何更改此 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

我的代码如下:

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 可能并不总是 DeliverSM。

试试这个:

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

如您所见,SMSC 正在发送心跳信号 (EnquireLink),因此您也必须对这些信号做出肯定答复。如果您不确认心跳,服务器将认为连接失效并关闭它(解除绑定)。