我有一个Tcp客户端服务器,当客户端和服务器之间的功能完成时,我添加了一个SSLstream用于连接
客户:
public void ReceiveMessage()
{
var clientCertificate = new X509Certificate2(ClientCertificateFile, ClientCertificatePassword);
var clientCertificateCollection = new X509CertificateCollection(new X509Certificate[] { clientCertificate });
string ServerCertificateName = "MyServer";
sslStream = new SslStream(this.GetStream(), false, App_CertificateValidation);
sslStream.AuthenticateAsClient(ServerCertificateName, clientCertificateCollection, SslProtocols.Tls12, false);
while (!SLLConnectionSuccess)
{
if (sslStream.IsAuthenticated)
{
SLLConnectionSuccess = true;
break;
}
}
while (!ClosedThreads)
{
byte[] buffer = new byte[1];
int bytes = -1;
try
{
bytes = sslStream.Read(buffer, 0, buffer.Length);
//Actions..
}
catch()
{
}
}
}
服务器:
public void Process()
{
Stream = client.GetStream();
using (var sslStream = new SslStream(Stream, false, App_CertificateValidation))
{
sslStream.AuthenticateAsServer(serverCertificate, true, SslProtocols.Tls12, false);
sslstream = sslStream;
while (true)
{
byte[] buffer = new byte[1];
try
{
sslStream.Read(buffer, 0, buffer.Length);
//Actions
}
catch(System.IO.IOException e)
{
}
Thread.Sleep(5);
}
}
}
当服务器崩溃并且客户端理解并终止连接时,我无法解决问题。使用TCP时,我使用了ping,但是在这里不起作用。我将不胜感激。