Anjay Ranjith
Anjay Ranjith

Reputation: 13

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.

Also can anyone tell me how to make changes that this java code captures only messages sent yo a particular 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

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

Upvotes: 0

Views: 1908

Answers (1)

Marvin
Marvin

Reputation: 658

PDU may not be always 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. In case you dont acknowledge heartbeats, server will think connection is deas and will close it (Unbind).

Upvotes: 0

Related Questions