是什么导致这个system.componentModel.Win32Exception:...通过sslstream发送消息?

时间:2012-03-05 21:56:54

标签: c# ssl ssl-certificate

由于某种原因,我是一个内部异常:system.componentModel.Win32Exception:客户端和服务器无法通信,因为它们没有通用的算法。

服务器端代码

  public void sendClientTypeRequest()
  {
        try
        {
            stream.AuthenticateAsServer(certInfo.cert, false, SslProtocols.Default, true);
            stream.ReadTimeout = 10000;
            stream.WriteTimeout = 10000;
            Messages.clientType clientType = Messages.clientType.request;
            sendMessage(clientType);
            logMessage("[" + DateTime.Now + "] ", ConsoleColor.Gray);
            logMessage(">> ", ConsoleColor.White);
            logMessage("Sending Client Type Request", ConsoleColor.Gray);
        }
        catch (AuthenticationException e)
        {
            logMessage(e.Message + "\n\n", ConsoleColor.Red);
            if (e.InnerException != null)
                logMessage(e.InnerException.ToString() + "\n\n", ConsoleColor.Red);
            socket.Close();
        }
   }

    private void sendMessage(object message)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        MemoryStream memory = new MemoryStream();
        formatter.Serialize(memory, message);
        byte[] sendBytes = memory.ToArray();
        stream.Write(sendBytes);
    }

客户端代码

void Start()
{
    socket = new TcpClient();
    socket.Connect(IP, Port);
    stream = new SslStream(socket.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate),null);
    try
    {
        stream.AuthenticateAsClient("servername");
    }
    catch(AuthenticationException e)
    {
        Console.write(e.ToString());
        socket.Close();
    }
    Thread listenerThread = new Thread(listener);
    listenerThread.Start();
}

0 个答案:

没有答案