Establishing a connection and sending a message

Sep 10, 2012 at 2:40 PM

Hi,

so following the steps for successfully establishing a connection and sending a message as listed on the website we have been unable to send message. we keep receiving a null reference client.SendMessage(message). following the source code it seems SMPPClientSession vTrans is not initialized but we are unable to resolve this issue. Kindly assist us on this

Thanks

Coordinator
Sep 10, 2012 at 5:54 PM
Could you post the source code here? That might help pin pointing the problem
Sep 10, 2012 at 6:36 PM

yes sure thing

            var client = new SmppClient();

            var properties = client.Properties;

            properties.SystemID = "systemid";
            properties.Password = "password";
            properties.Port = 5000;
            properties.Host = "192.168.0.1";
            properties.SystemType = "SMPP";
           // properties.DefaultServiceType = "sms-service";



            client.AutoReconnectDelay = 3000;
            client.KeepAliveInterval = 15000;


            

            client.ConnectionStateChanged += (sender, e) =>
            {

                switch (e.CurrentState)
                {

                    case SmppConnectionState.Closed:
                        // Connection to the remove server is lost

                        e.ReconnectInteval = 60000;
                        break;

                    case SmppConnectionState.Connected:

                        break;

                    case SmppConnectionState.Connecting:
                        break;


                }

            };



            // Start smpp client
            client.Start();
            

            var message = new TextMessage();

            message.DestinationAddress = "265999245196";
            
            message.SourceAddress = "TEST";
            message.Text = "Hello, this is my test message!";

            client.SendMessage(message); // NullReference exception is thrown here
r

Coordinator
Sep 11, 2012 at 4:37 AM

I am not sure why the null reference exception is being raised. Could you put a line in the ConnectionStateChanged event and send outputs to the console when connection is established/lost?

Also, could you try to debug the source code and put break points at all places where vTrans variable is assigned?

Sep 11, 2012 at 7:48 AM

Having put Console outputs on ConnectionStateChanged event it seems their isn't any output at all.  After putting a breakpoint from the Start method of SmppClient class it seems in RaiseStateChangedEvent method StateChanged appear to be null

private void RaiseStateChangedEvent(bool started)
        {
            if (StateChanged == null) { return; } // The method breaks here because StateChanged is null
            StateChangedEventArgs e = new StateChangedEventArgs(started);
            StateChanged(this, e);
        }

vTrans is intialized in OpenSession method of SmppClient but when I put a breakpoint the code seems to be unreachable as OpenSession is
not called at all
private void OpenSession(SessionBindInfo bindInfo, bool useSeparateConnections, int timeOut)
        {
            ChangeState(SmppConnectionState.Connecting);
            if (useSeparateConnections)
            {
                //Create two separate sessions for sending and receiving
                try
                {
                    bindInfo.AllowReceive = true;
                    bindInfo.AllowTransmit = false;
                    vRecv = SmppClientSession.Bind(bindInfo, timeOut);
                    InitializeSession(vRecv);
                }
                catch
                {
                    ChangeState(SmppConnectionState.Closed);
                    //Start reconnect timer
                    StartTimer();
                    throw;
                }
                //--
                try
                {
                    bindInfo.AllowReceive = false;
                    bindInfo.AllowTransmit = true;
                    vTrans = SmppClientSession.Bind(bindInfo,  timeOut); // Initialization happens here 
                    InitializeSession(vTrans);
                }
                catch
                {
                    try { vRecv.EndSession(); }
                    catch {/*Silent catch*/}
                    vRecv = null;
                    ChangeState(SmppConnectionState.Closed);
                    //Start reconnect timer
                    StartTimer();
                    throw;
                }
                ChangeState(SmppConnectionState.Connected);
            }
            else
            {
                //Use a single session for both sending and receiving
                bindInfo.AllowTransmit = true;
                bindInfo.AllowReceive = true;
                try
                {
                    SmppClientSession session = SmppClientSession.Bind(bindInfo, timeOut);
                    vTrans = session;
                    vRecv = session;
                    InitializeSession(session);
                    ChangeState(SmppConnectionState.Connected);
                }
                catch (SmppException ex)
                {
                    if (ex.ErrorCode == SmppErrorCode.ESME_RINVCMDID)
                    {
                        //If SMSC returns ESME_RINVCMDID (Invalid command id)
                        //the SMSC might not be supporting the BindTransceiver PDU
                        //Therefore, we can try to use bind with separate connections
                        OpenSession(bindInfo, true, timeOut);
                    }
                    else
                    {
                        ChangeState(SmppConnectionState.Closed);
                        //Start background timer
                        StartTimer();
                        throw;
                    }
                }
                catch
                {
                    ChangeState(SmppConnectionState.Closed);
                    StartTimer();
                    throw;
                }
            }
        }


Sep 11, 2012 at 9:57 AM

It happens because you're sending message while client is not connected yet. And SendMessage(ShortMessage message) overload does not check connection state (I'd say it is a bug). Try waiting until client is connected:

 

client.Start();

while (client.ConnectionState != SmppConnectionState.Connected)
{
    Console.WriteLine(client.ConnectionState + " waiting...");
    Thread.Sleep(1);
}

var message = new TextMessage();

 

 

Coordinator
Sep 11, 2012 at 12:08 PM
Try to follow oderibas' advice. Make sure the client is connected before sending message.
Sep 11, 2012 at 12:47 PM

I tried the above code and now I'm able to connect but public the SendMessage method exits prematurely without any errors or exception thrown

Sep 11, 2012 at 12:54 PM

Why do you think it exits prematurely?

SendMessage method doesn't have any return value so if it doesn't throw exception - it means that message was sent successfully.

Sep 11, 2012 at 1:27 PM

I'm not sure why exactly why this is happening. The destination number is my own cell phone number and I was unable to receive the message. The SMPP server we are using seems to be working with no issues as well

Coordinator
Sep 12, 2012 at 3:58 AM
Try to work closely with your SMPP service provider (on the server side). Sometimes it is a configuration issue.
In most cases SMPP service providers should be able to debug PDUs and tell where the problem is.