Esempio n. 1
0
            static void ProcessReceiveResults(IAsyncResult ar)

            {
                TcpClientHandler TCH = (TcpClientHandler)ar.AsyncState;

                try

                {
                    int BytesRead = 0;

                    BytesRead = TCH.TcpClient.GetStream().EndRead(ar);

                    //Console.WriteLine("Connection " + TCH.ConnectionId.ToString() + " received " + BytesRead.ToString() + " byte(s).");



                    if (BytesRead == 0)

                    {
                        //Console.WriteLine("Connection " + TCH.ConnectionId.ToString() + " is closing.");

                        //NSH.NetworkStream.Close();
                        TCH.Close();

                        DateTime dtEnd = DateTime.Now;

                        return;
                    }

                    else
                    {
                        string Data = System.Text.Encoding.GetEncoding(28591).GetString(TCH.Buffer);
                        //Console.Write(Data);

                        foreach (char DataCharIn in Data)
                        {
                            TCH.ProcessDataReceived(DataCharIn);
                        }
                    }

                    if (TCH.Connected == true)
                    {
                        TCH.Buffer = new byte[BufferSize];
                        TCH.TcpClient.GetStream().BeginRead(TCH.Buffer, 0, TCH.Buffer.Length, TCH.AsyncReceiveCallback, TCH);
                    }
                }

                catch (Exception e)

                {
                    if (TCH.Connected == true)
                    {
                        Console.WriteLine("Failed to read from a network stream with error: " + e.Message);

                        //TCH.NetworkStream.Close();
                        TCH.Close();
                    }
                }
            }