ASPX connecting to SMSC problem (when debugging, everything ok, but if you just run it, execution skips some codes)

Aug 17 2007 3:18 AM

Sorry if this may sound stupid or something, I am just new to developing web applications using .NET. I have a web page that connects to a SMSC for sending SMS spiels.

At first, i tried to put everything(connection, sending and receiving to SMSC) inside Page Load. This works just fine. But when i tried to place my SMSC functionalities inside classes I encountered a problem.

Problem : When i put break points on the lines in my page load and start to run and DEBUG my web page, everything works out fine. But when I delete the break points, the flow seems to skip SMSC functionalities(the classes).


Friend.aspx (code snippet from Page_Load)
TcpClient tcpc = smscc.ConnectClient(ConfigurationManager.AppSettings.Get("SMSCServer"),
                    int.Parse(ConfigurationManager.AppSettings.Get("SMSCPort")));
NetworkStream netStream = smscc.getConnectedNetworkStream();
if (netStream == null)
{
    netStream = tcpc.GetStream();
}

StatusLabel.Text = cimdio.readStreamBeforeLogin(netStream, smscc.readBuffer);
string loginMessage = cimdm.createLoginParams(ConfigurationManager.AppSettings.Get("SMSCUsername"), ConfigurationManager.AppSettings.Get("SMSCPassword"));
bool tempBool = cimdio.isSuccessfulLogin(netStream, smscc.writeBuffer, smscc.readBuffer, loginMessage);
if (tempBool)
{
                string submitMessage = cimdm.createSubmitParams(senderMIN, "222", "00", "00", ConfigurationManager.AppSettings.Get("SenderSpiel"));
                cimdio.sendMessage(netStream, smscc.writeBuffer, submitMessage);
                string submitMessage2 = cimdm.createSubmitParams(friendMIN, "222", "00", "00", ConfigurationManager.AppSettings.Get("FriendSpiel"));
                cimdio.sendMessage(netStream, smscc.writeBuffer, submitMessage2);
                StatusLabel.Text = "sent messages";
}
string logoutMessage = cimdm.createLogoutParams();
cimdio.logout(tcpc, netStream, smscc.writeBuffer, logoutMessage);


public class SMSCConnection
{
    private TcpClient client = new TcpClient();
    private NetworkStream clientStream;
    public byte[] writeBuffer;
    public byte[] readBuffer = new byte[2048];

    public TcpClient ConnectClient(string uid, int pw)
    {
        client.Connect(uid, pw);
        return client;
    }

    public NetworkStream getConnectedNetworkStream()
    {
        if (this.client.Connected)
        {
            clientStream = this.client.GetStream();
            return clientStream;
        }
        else
        {
            clientStream = null;
            return clientStream;
        }
    }
}


public class CIMDMessages
    {
        private const int _tab = 9;
        private const int _startText = 2;
        private const int _endText = 3;
       
        private string message;
        private string _packetNumber = "000";

        private string Chr(int p_intVal)
        {
            byte[] bytData = new byte[] { (byte)p_intVal };
            return Encoding.GetEncoding(1252).GetString(bytData);
        }

        //CIMDMessage cimdm = new CIMDMessage();
        public string createLoginParams(string uid, string pw)
        {
            string tempMessage = String.Format("010:{0}{1}011:{2}", uid, Chr(_tab), pw);
            tempMessage = appendCIMDHeader(OperationCode.login, tempMessage);
            this.message = appendCIMDTrailerTabbed(tempMessage);
            return this.message;
        }


        public string createSubmitParams(string destNumber, string senderNumber, string tariffClass, string serviceDesc, string userData)
        {
            string tempMessage = String.Format("021:{0}{1}023:{2}{3}064:{4}{5}065:{6}{7}033:{8}", destNumber, Chr(_tab), senderNumber, Chr(_tab), tariffClass, Chr(_tab), serviceDesc, Chr(_tab), userData);
            tempMessage = appendCIMDHeader(OperationCode.submit, tempMessage);
            this.message = appendCIMDTrailerTabbed(tempMessage);
            return this.message;
        }


        public string createLogoutParams()
        {
            string tempMessage = "";
            tempMessage = appendCIMDHeader(OperationCode.logout, tempMessage);
            this.message = appendCIMDTrailer(tempMessage);
            return this.message;
        }

        private int getOpCode(OperationCode oper)
        {
            int operationCode = 0;
            switch (oper.ToString())
            {
                case "login":
                    operationCode = 1;
                    break;
                case "logout":
                    operationCode = 2;
                    break;
                case "submit":
                    operationCode = 3;
                    break;
                case "enquire":
                    operationCode = 4;
                    break;
                case "deliveryrequest":
                    operationCode = 5;
                    break;
                case "cancel":
                    operationCode = 6;
                    break;
                case "delivermessage":
                    operationCode = 20;
                    break;
                case "deliverstatusreport":
                    operationCode = 23;
                    break;
                case "set":
                    operationCode = 8;
                    break;
                case "get":
                    operationCode = 9;
                    break;
                case "alive":
                    operationCode = 40;
                    break;
            }
            return operationCode;
        }

        private string appendCIMDHeader(OperationCode opType, string paramMessage)
        {
            string _messageHeader;
            if (_packetNumber.Equals("000"))
            {
                _packetNumber = "001";
            }
            else
            {
                try
                {
                    int packetNumberInt = int.Parse(_packetNumber);
                    packetNumberInt = packetNumberInt + 2;
                    _packetNumber = packetNumberInt.ToString();

                    //try to append zero's if length less than 3
                    if (_packetNumber.Length < 3)
                    {
                        _packetNumber = _packetNumber.PadLeft(3, '0');
                    }
                }
                catch (FormatException)
                {
                    _packetNumber = "001";
                }
            }
            string operationCode = getOpCode(opType).ToString();
            if (operationCode.Length < 2)
            {
                operationCode = operationCode.PadLeft(2, '0');
            }
            ////////////////////////////
            _messageHeader = Chr(_startText) + operationCode + ":" + _packetNumber + Chr(_tab);
            return _messageHeader + paramMessage;
        }

        private string appendCIMDTrailerTabbed(string paramMessage)
        {
            return paramMessage + Chr(_tab) + Chr(_endText);
        }

        private string appendCIMDTrailer(string paramMessage)
        {
            return paramMessage + Chr(_endText);
        }
    }




public class CIMDIO
    {
        public void sendMessage(NetworkStream netStream, byte[] writeBuffer, string message)
        {
            writeBuffer = Encoding.ASCII.GetBytes(message);
            netStream.Write(writeBuffer, 0, writeBuffer.Length);
        }

        public bool isSuccessfulLogin(NetworkStream netStream, byte[] writeBuffer, byte[] readBuffer, string loginMessage)
        {
            StringBuilder loginResultMessage = new StringBuilder();
            writeBuffer = Encoding.ASCII.GetBytes(loginMessage);
            netStream.Write(writeBuffer, 0, writeBuffer.Length);

            if (netStream.CanRead)
            {
                int numberOfBytesRead = 0;
                do
                {
                    numberOfBytesRead = netStream.Read(readBuffer, 0, readBuffer.Length);
                    loginResultMessage.AppendFormat("{0}", Encoding.ASCII.GetString(readBuffer, 0, numberOfBytesRead));
                }
                while (netStream.DataAvailable);

                if (loginResultMessage.ToString().Contains("51:001"))
                    return true;
                else
                    return false;
            }
            else
            {
                return false;
            }

        }
       
        public string readStreamBeforeLogin(NetworkStream netStream, byte[] readBuffer)
        {
            StringBuilder loginResultMessage = new StringBuilder();
            if (netStream != null)
            {
                while (netStream.DataAvailable)
                {
                    int numberOfBytesRead = netStream.Read(readBuffer, 0, readBuffer.Length);
                    loginResultMessage.AppendFormat("{0}", Encoding.ASCII.GetString(readBuffer, 0, numberOfBytesRead));
                }
                return loginResultMessage.ToString();
            }
            else
                return "";

        }

        public void logout(TcpClient tcpc, NetworkStream netStream, byte[] writeBuffer, string logoutMessage)
        {
            sendMessage(netStream, writeBuffer, logoutMessage);
            netStream.Close();
            tcpc.Close();
        }
    }



PLEASE HELP.