我已经构建了一个java程序来监听SMPP服务器并捕获发送到该服务器的SMS它工作正常但是在一定的时间间隔后我得到了不同类型的错误,如下所示。
也可以有人告诉我如何更改此java代码仅捕获特定SC发送的消息
错误:com.logica.smpp.pdu.EnquireLink无法转换为 com.logica.smpp.pdu.DeliverSM错误:com.logica.smpp.pdu.Unbind不能 被投射到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();
}
}
}
答案 0 :(得分:0)
PDU可能不总是DeliverSM。
试试这个:
PDU pdu = session.receive(1500);
if ((pdu != null) && (pdu instanceof DeliverSM)) {
...
DeliverSM sms = (DeliverSM) pdu;
如您所见,SMSC正在发送Heartbeats(EnquireLink),因此您必须对这些内容给予肯定回答。如果你不承认心跳,服务器会认为连接是deas并将关闭它(Unbind)。