C# SslStream无法从传输连接读取数据
我试图在安全流层中使用AsyncWait,但我面临这个问题C# SslStream无法从传输连接读取数据,c#,tcpclient,sslstream,C#,Tcpclient,Sslstream,我试图在安全流层中使用AsyncWait,但我面临这个问题 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; usi
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();
}
}
}
使用系统;
Net系统;
使用System.Net.Security;
使用System.Net.Sockets;
使用System.Security.Cryptography.X509证书;
使用System.Threading.Tasks;
命名空间异步等待
{
内部课程计划
{
私有静态只读X509Certificate X509Cert=X509Certificate.CreateFromCertFile(“crt.cer”);
私有静态void Main()
{
Console.SetWindowsSize(100,30);
CreateListener();
对于(变量i=0;i<1;i--)
Console.ReadLine();
}
静态异步void CreateListener()
{
var listener=new TcpListener(new IPEndPoint(IPAddress.Any,5070));
listener.Start();
WriteLine(“TcpListener启动”);
做
{
IClient client=newbaseclient(wait listener.AcceptTcpClientAsync());
认证和开始(客户);
}而(!listener.Pending());
}
静态异步void AuthenticateAndBeginRead(IClient客户端)
{
尝试
{
等待client.Stream.authenticatesServerAsync(X509Cert);
if(client.Stream.IsAuthenticated)
做
{
var received=await ReadStream(客户机,client.Buffer);
如果(接收==null)
抛出新异常();
日志写入包(已收到);
}while(client.Stream.CanRead);
其他的
{
抛出新异常();
}
}
捕获(例外e)
{
//我不想捕捉强制关闭异常,也不想捕捉其他无用的异常!>。
Log.WriteLine(e.Message);
}
}
专用静态异步任务读取流(IClient客户端,字节[]缓冲区)
{
var size=await client.Stream.ReadAsync(buffer,0,buffer.Length);
if(size.Equals(0))返回null;
var endBuffer=新字节[大小];
块复制(Buffer,0,endBuffer,0,size);
Array.Clear(buffer,0,buffer.Length);
返回端缓冲区;
}
}
接口客户端
{
字节[]缓冲区{get;}
SslStream流{get;}
字符串Ip{get;}
}
类BaseClient:IClient
{
公共字节[]缓冲区{get;private set;}
公共SslStream流{get;private set;}
公共字符串Ip{get;private set;}
公共BaseClient(TcpClient客户端)
{
缓冲区=新字节[ushort.MaxValue];
Stream=newsslstream(client.GetStream());
Ip=((IPEndPoint)client.client.RemoteEndPoint).Address.ToString();
}
}
}
如果您发现其他可疑的编程错误,也请纠正我