Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

SMPP Sending Message

843785Dec 7 2008 — edited Dec 11 2008
Hi there!

I need your help guys!

I am using SMPP API from Logica SMS. There is no forum on their site. so need your help.

Here is my problem, I am connecting to a SMSC server and I am able to connect to the SMPP server and receive SMS but not able to send any. The code I am going to attach is a Demo with slight modification from Logica.

Need you help
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package smpp;

/**
 *
 * @author it's me
 */
import ie.omk.smpp.*;
import ie.omk.smpp.UnsupportedOperationException;
import ie.omk.smpp.event.*;
import ie.omk.smpp.message.*;
import ie.omk.smpp.version.VersionException;
import java.io.IOException;
import java.net.*;
import org.apache.log4j.*;

public class SMSDemo extends Thread implements ConnectionObserver {

    private Connection conn;
    private boolean exit = false;
    private static SMSDemo instance;
    Logger log = Logger.getLogger(SMSDemo.class);

    private SMSDemo() {
        SimpleLayout layout = new SimpleLayout();

        FileAppender appender = null;
        try {
            appender = new FileAppender(layout, "output1.txt", false);
        } catch (Exception e) {
        }

        log.addAppender(appender);
        log.setLevel(Level.INFO);
    }

    public static SMSDemo getInstance() {
        if (instance == null) {
            instance = new SMSDemo();
        }
        return instance;
    }

    private void connect() {
        try {
            //conn = new Connection("localhost", 2775, true);
            conn = new Connection("serverip", 1111, true);
            conn.autoAckLink(true);
            conn.autoAckMessages(true);
            conn.addObserver(this);
        } catch (UnknownHostException uhe) {
            System.exit(0);
        }

        boolean retry = false;

        while (!retry) {
            try {
                conn.bind(Connection.TRANSCEIVER, "uname", "pwd", null);
                retry = true;
            } catch (IOException ioe) {
                try {
                    sleep(60 * 1000);
                } catch (InterruptedException ie) {
                }
            }
        }
    }

    public void run() {
        while (!exit) {
            connect();
            synchronized (this) {
                try {
                    wait();
                } catch (InterruptedException ie) {
                }
            }
        }
    }

    public void update(Connection conn, SMPPEvent ev) {
        if (ev.getType() == SMPPEvent.RECEIVER_EXIT && ((ReceiverExitEvent) ev).isException()) {
            synchronized (this) {
                notify();
            }
        }
    }

    public void packetReceived(Connection conn, SMPPPacket pack) {
        switch (pack.getCommandId()) {
            case SMPPPacket.DELIVER_SM:
                try {
                    SubmitSM response = processRequest(pack);
                    SubmitSMResp smr = (SubmitSMResp) conn.sendRequest(response);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case SMPPPacket.BIND_TRANSCEIVER_RESP:
                if (pack.getCommandStatus() != 0) {
                    System.out.println("Error binding: " + pack.getCommandStatus());
                    exit = true;
                    synchronized (this) {
                        notify();
                    }
                } else {
                    System.out.println("Bounded");
                }
        }
    }

    private SubmitSM processRequest(SMPPPacket request) throws BadCommandIDException {
        //SubmitSM sm = (SubmitSM) conn.newInstance(SMPPPacket.SUBMIT_SM);
        //sm.setDestination(request.getSource());

        String msgid = request.getMessageId();
        String msgsType = request.getServiceType();
        String msgsdest = request.getDestination().getAddress();
        String msgSource = request.getSource().getAddress();
        System.out.println(msgSource + " " + msgsdest);


        logPacket(request, "IN");

        System.out.println("sending");
        SubmitSM sm = (SubmitSM) conn.newInstance(SMPPPacket.SUBMIT_SM);
        if (msgSource.contains("mynumber e.g 098776666565")) {
            try {
                Address destination = new Address(
                        1,
                        1, 
                        msgSource);

                sm.setDestination(destination);
                sm.setMessageText("Test Short Message. :-)");
                conn.sendRequest(sm);
            } catch (SocketTimeoutException ex) {
                log.warn(ex.getStackTrace());
            } catch (IOException ex) {
                log.warn(ex.getStackTrace());
            } catch (AlreadyBoundException ex) {
                log.warn(ex.getStackTrace());
            } catch (VersionException ex) {
                log.warn(ex.getStackTrace());
            } catch (SMPPProtocolException ex) {
                log.warn(ex.getStackTrace());
            } catch (UnsupportedOperationException ex) {
                log.warn(ex.getStackTrace());
            }
        }

        logPacket(sm, "OUT");

        return sm;
    }

    private void logPacket(SMPPPacket packet, String direction) {
        String phone;
        if (direction.equals("OUT")) {
            phone = packet.getDestination().getAddress();
        } else {
            phone = packet.getSource().getAddress();
        }
        System.out.println(direction + ": " + phone + " - " + packet.getMessageText());
    }

    public Connection getConnection() {
        return conn;
    }

    public static void main(String[] args) {
        Runtime.getRuntime().addShutdownHook(new Hook());
        SMSDemo demo = SMSDemo.getInstance();
        demo.start();
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 8 2009
Added on Dec 7 2008
5 comments
432 views