⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmpp2recive.cs

📁 这是一个很好的收发短信程序 需要都尽情享用
💻 CS
字号:
namespace Glenet.CMPP
{
	using System;
	using System.Text;
	using System.Net.Sockets;
	using System.Threading;
	using Glenet.CMPP.Messages;

    public class CMPP2Recive : BaseCMPP2
	{

        public CMPP2Recive(string userName, string passwd, string spCode, string getwayip, int port, string serviceNo)
            : base(userName, passwd, spCode, getwayip, port, serviceNo)
        {
        }

        //声明短信到达处理事件
        public delegate void CMPP_DeliverEventHandler(CMPP_DELIVER cmpp_deliver);
        public event CMPP_DeliverEventHandler CMPP_Deliver;

        private Thread th = null;
        /// <summary>
        /// 开始接收消息
        /// </summary>
        public void Start()
        {
            th = new Thread(new ThreadStart(Deliver));
            th.Start();
        }
        /// <summary>
        /// 接收消息
        /// </summary>
        public void Deliver()
        {
            if (Ns == null)
            {
                if(!Connection())
                    throw new Exception("建立连接失败");
            }
            try
            {
                int s = 10; //buffer size
                byte[] bytes = new byte[400];
                while (true)
                {
                    if (Ns.CanRead)
                    {
                        if (Ns.DataAvailable)
                        {

                            byte[] buffer = null;
                            buffer = ResponseAsBytes(Ns, bytes.Length, s);

                            if (buffer.Length > 0)
                            {
                                CMPP_DELIVER deliver = new CMPP_DELIVER(buffer);
                                //PrintHeader(deliver.Header);
                                if (CMPP_Deliver != null)
                                {
                                    CMPP_Deliver(deliver);
                                }
                                if (Ns.CanWrite)
                                {
                                    CMPP_DELIVER_RESP deliver_resp = new CMPP_DELIVER_RESP(deliver.Msg_Id, 0);
                                    buffer = deliver_resp.ToBytes();
                                    Ns.Write(buffer, 0, buffer.Length);
                                }

                            }

                        }
                    }
                    Thread.Sleep(100);
                }
            }
            catch
            {
                Close();
                Start();
            }
            
        
        }
        /// <summary>
        /// 重载关闭连接
        /// </summary>
        public override void Close()
        {
            if (th != null)
            {
                th.Abort();
                th.Join();
            }
            base.Close();
        }

        ////短信到达事件
        //private static void ConsoleApplication_CMPP_Deliver(CMPP_DELIVER deliver)
        //{
        //    //PrintHeader(deliver.Header);
        //    string s = "";
        //    if (deliver.Registered_Delivery == 0) //普通短信
        //    {
        //        s = String.Format("普通短信: {0}\n{1}", deliver.Src_terminal_Id, deliver.Msg_Content);
        //    }
        //    else
        //        //该模拟器不能自动生成状态报告再下发!请自行键入下面短信内容后,发送
        //        //状态报告短信: 00000001DELIVRD031213505003121350501391xxx11381391xxx11381391xx11380001
        //    {
        //        CMPP_Msg_Content x = new CMPP_Msg_Content(Encoding.ASCII.GetBytes(deliver.Msg_Content));
        //        s = String.Format("状态报告: {0}\n{1}", x.Stat, x.Dest_terminal_Id);
        //    }
        //    System.Console.WriteLine(s);

        //}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -