Beispiel #1
0
        public Form1()
        {
            InitializeComponent();

            timer.Tick    += new System.EventHandler(this.sendTimer);
            timer.Interval = 2000;

            UIController.Instance.SubscribeRouteChangedMsg(AddText);

            netHelper = new SocketHelper("127.0.0.1", 2112);
            XConnection concreator = new PushMsgConnection();

            netHelper.SetConnection(concreator);
            netHelper.AsyncReceive();
        }
Beispiel #2
0
 void ReceiveCallback(IAsyncResult ar)
 {
     try
     {
         SocketHelper session   = (SocketHelper)ar.AsyncState;
         int          bytesread = m_Socket.EndReceive(ar);
         if (0 < bytesread)
         {
             //取最后四个字节判断是否为包尾,如果是则说明已经收到一个完整的数据包了
             if (session.IsIntegrity())
             {
                 m_Connection.Parse(Encoding.ASCII.GetString(Buffer, 0, bytesread));
                 this.AsyncReceive();
             }
             else
             {
                 m_Socket.BeginReceive(Buffer, 0, BufferSize, 0, new AsyncCallback(ReceiveCallback), session);
             }
         }
         else
         {
             if (session.IsIntegrity())
             {
                 this.AsyncReceive();
                 m_Connection.Parse(Buffer);
                 session = null;
             }
             else
             {
                 this.Close();
             }
         }
     }
     catch (Exception e)
     {
         //出现异常,关闭连接
         this.Close();
     }
 }