Ejemplo n.º 1
0
        private void OnAcceptTcpClient(IAsyncResult ar)
        {
            SampleService sv = (SampleService)(ar.AsyncState);

            if (sv.bStarted)
            {
                try
                {
                    sv.client     = sv.EndAcceptTcpClient(ar);
                    sv.bConnected = true;
                    sv.timer.Start();
                    //开始异步读取数据
                    sv.client.GetStream().BeginRead(sv.readbuffer1, 0, readbuffersize,
                                                    OnDataReceived, sv);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("AcceptTcpClient Error:" + ex.Message);
                }
            }
        }
Ejemplo n.º 2
0
        private void OnDataReceived(IAsyncResult ar)
        {
            SampleService sv = (SampleService)(ar.AsyncState);

            if (!sv.bStarted || sv.client == null)
            {
                return;
            }

            int recv = 0;

            try
            {
                recv = sv.client.GetStream().EndRead(ar);
            }
            catch (Exception ex)
            {
                recv = 0;
                Console.WriteLine("HandleDataReceived:" + ex.Message);
            }

            try
            {
                if (recv != 0)
                {
                    if (sv.bAuthed)
                    {
                        if (recv % 2 != 0)
                        {
                            Console.WriteLine("接收到:" + recv);
                        }

                        sv.curreadlen += recv;
                        if (sv.curreadlen == readbuffersize)
                        {
                            if (sv.readedbuffer == null)
                            {
                                sv.readedbuffer = sv.curreadbuffer;
                            }
                            else
                            {
                                Console.WriteLine("data lasted");
                            }
                            if (sv.curreadbuffer == sv.readbuffer1)
                            {
                                sv.curreadbuffer = sv.readbuffer2;
                            }
                            else
                            {
                                sv.curreadbuffer = sv.readbuffer1;
                            }
                            //static int fm = 0;
                            //Console::WriteLine("frame:" + (++fm));
                            sv.curreadlen = 0;
                        }

                        sv.client.GetStream().BeginRead(sv.curreadbuffer, sv.curreadlen,
                                                        readbuffersize - sv.curreadlen,
                                                        OnDataReceived, sv);
                    }
                    else
                    {
                        if (recv == 19)
                        {
                            byte[] temp = new byte[recv];
                            Array.Copy(sv.readbuffer1, temp, recv);
                            string sn = Encoding.Default.GetString(temp);
                            if (sn.CompareTo("sn:testtesttesttest") == 0)
                            {
                                sv.timer.Stop();
                                sv.bAuthed = true;
                                Console.WriteLine("验证成功");
                                byte[] res = Encoding.Default.GetBytes("snok");
                                sv.client.GetStream().BeginWrite(res, 0, res.GetLength(0), null, null);

                                sv.InitSample();

                                sv.client.GetStream().BeginRead(sv.curreadbuffer, 0, readbuffersize,
                                                                OnDataReceived, sv);

                                return;
                            }
                        }
                        Console.WriteLine("验证无效");
                        sv.bConnected = false;
                    }
                }
                else
                {
                    sv.bConnected = false;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("handle receive error:" + ex.Message);
            }
        }