1

我正在尝试在安全流层中使用异步等待,但我遇到了这个问题

Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

我如何处理关闭异常

这是我的代码

using System;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;

namespace asyncAwait
{
    internal class Program
    {
        private static readonly X509Certificate X509Cert = X509Certificate.CreateFromCertFile("crt.cer");

        private static void Main()
        {
            Console.SetWindowSize(100, 30);
            CreateListener();

            for (var i = 0; i < 1; i--)
                Console.ReadLine();
        }

        static async void CreateListener()
        {
            var listener = new TcpListener(new IPEndPoint(IPAddress.Any, 5070));
            listener.Start();
            Log.WriteLine("TcpListener started.");

            do
            {
                IClient client = new BaseClient(await listener.AcceptTcpClientAsync());
                AuthenticateAndBeginRead(client);
            } while (!listener.Pending());
        }

        static async void AuthenticateAndBeginRead(IClient client)
        {
            try
            {
                await client.Stream.AuthenticateAsServerAsync(X509Cert);

                if (client.Stream.IsAuthenticated)
                    do
                    {
                        var received = await ReadStream(client, client.Buffer);
                        if (received == null)
                            throw new Exception();

                        Log.WritePacket(received);
                    } while (client.Stream.CanRead);
                else
                {
                    throw new Exception();
                }
            }
            catch (Exception e)
            {
                //I don't wanna to catch the force closing exception nor the other useless ones!>.
                Log.WriteLine(e.Message);
            }
        }

        private static async Task<byte[]> ReadStream(IClient client, byte[] buffer)
        {
            var size = await client.Stream.ReadAsync(buffer, 0, buffer.Length);
            if (size.Equals(0)) return null;
            var endBuffer = new byte[size];
            Buffer.BlockCopy(buffer, 0, endBuffer, 0, size);
            Array.Clear(buffer, 0, buffer.Length);
            return endBuffer;
        }
    }


    interface IClient
    {
        byte[] Buffer { get; }
        SslStream Stream { get; }
        string Ip { get; }
    }

    class BaseClient : IClient
    {
        public byte[] Buffer { get; private set; }
        public SslStream Stream { get; private set; }
        public string Ip { get; private set; }

        public BaseClient(TcpClient client)
        {
            Buffer = new byte[ushort.MaxValue];
            Stream = new SslStream(client.GetStream());
            Ip = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString();
        }
    }
}

如果您发现其他可疑的编程错误,也请纠正我。

4

0 回答 0