1

我正在为 iPhone 构建一个客户端-服务器应用程序,服务器是用 c# 编写的,并使用 TcpListener 来监听传入的连接,并使用 TcpClient 来处理每个连接。代码看起来像这样:

    private void startAcceptingConnections()
    {
        m_Listener = new TcpListener(i_IPAddress, i_Port);
        m_Listener.Start();

        while (true)
        {
            //blocking
            TcpClient tcpConnection = m_Listener.AcceptTcpClient();

            Thread ClientAuthenticationThread = new Thread(new ParameterizedThreadStart(HandleClientConnection));
            ClientAuthenticationThread.Start(tcpConnection);
        }

    }

    private void HandleClientConnection(object i_Client)
    {
        TcpClient client = i_Client as TcpClient;

        if (client != null)
        {
            NetworkStream clientStream = client.GetStream();
            byte[] buffer = new byte[4096];
            int bytesRead = 0;

            //blocking
            bytesRead = clientStream.Read(buffer, 0, 4096);
            if (bytesRead == 0)
            {
                client.Close();
            }
            else
            {
                // do something with bytesRead
            }
        }
    }

我想将 SSL 添加到我的应用程序中,所以这是我到目前为止所做的:

  1. 在我的 MacBook 中,我使用 Keycahin 创建了一个证书颁发机构
  2. 然后使用我在第一步中创建的 CA,我颁发了服务器 SSL 证书(我认为它称为服务器身份)
  3. 我将 CA 捆绑在我的 iPhone 应用程序中。
  4. 我将服务器身份导出为 .p12 文件

现在我知道我需要在我的服务器中安装这个文件,但我不知道如何。有人可以指导我吗?

@@@@@ 我在 MSDN 中找到了这段代码,我只需要添加这些代码来支持 SSL 吗?

private void startAcceptingConnections()
{
    string certPath = "C:\\...Path...\\ServerCertificates.p12";
    serverCertificate = new X509Certificate(certPath, "serverCertPassword");

    m_Listener = new TcpListener(i_IPAddress, i_Port);
    m_Listener.Start();

    while (true)
    {
        //blocking
        TcpClient tcpConnection = m_Listener.AcceptTcpClient();

        Thread ClientAuthenticationThread = new Thread(new ParameterizedThreadStart(HandleClientConnection));
        ClientAuthenticationThread.Start(tcpConnection);
    }

}

private void HandleClientConnection(object i_Client)
{
    TcpClient client = i_Client as TcpClient;

    try
    {
        NetworkStream clientStream = client.GetStream();

        // A client has connected. Create the 
        // SslStream using the client's network stream.
        SslStream sslStream = new SslStream(clientStream, false);

        sslStream.AuthenticateAsServer(serverCertificate, false, SslProtocols.Tls, true);

        byte[] buffer = new byte[4096];
        int bytesRead = 0;

        //blocking
        bytesRead = clientStream.Read(buffer, 0, 4096);
        if (bytesRead == 0)
        {
            sslStream.Close();
            client.Close();
        }
        else
        {
            // do something with bytesRead
        }
    }
    catch (AuthenticationException e)
    {
        Console.WriteLine("Exception: {0}", e.Message);
        if (e.InnerException != null)
        {
            Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
        }
        Console.WriteLine ("Authentication failed - closing the connection.");
        sslStream.Close();
        client.Close();
        return;
    }
}
4

0 回答 0