temz 2023-03-24 10:21 采纳率: 100%
浏览 80
已结题

求个C# SSL socket的客户端和 服务端代码

求个C# SSL socket的客户端和 服务端代码,得能连的通的

  • 写回答

5条回答 默认 最新

  • 「已注销」 2023-03-24 10:39
    关注

    参考GPT:下面是一个简单的 C# SSL socket 客户端和服务端的示例代码,可以用于测试 SSL socket 的连接。需要注意的是,此示例代码中使用了自签名证书,如果您要在生产环境中使用 SSL socket,请使用受信任的证书。

    服务端代码:

    using System;
    using System.IO;
    using System.Net;
    using System.Net.Security;
    using System.Net.Sockets;
    using System.Security.Authentication;
    using System.Security.Cryptography.X509Certificates;
    using System.Text;
    
    class Server
    {
        static void Main(string[] args)
        {
            TcpListener listener = new TcpListener(IPAddress.Any, 8888);
            listener.Start();
            Console.WriteLine("Server started, waiting for connections...");
    
            while (true)
            {
                TcpClient client = listener.AcceptTcpClient();
                Console.WriteLine("Client connected: " + client.Client.RemoteEndPoint.ToString());
    
                SslStream stream = new SslStream(client.GetStream(), false);
                X509Certificate2 cert = new X509Certificate2("server.pfx", "password");
                stream.AuthenticateAsServer(cert, false, SslProtocols.Tls, true);
    
                StreamReader reader = new StreamReader(stream, Encoding.UTF8);
                StreamWriter writer = new StreamWriter(stream, Encoding.UTF8) { AutoFlush = true };
    
                writer.WriteLine("Welcome to SSL server!");
                string message = reader.ReadLine();
                Console.WriteLine("Received message from client: " + message);
    
                writer.WriteLine("Server received your message: " + message);
                stream.Close();
                client.Close();
                Console.WriteLine("Client disconnected.");
            }
        }
    }
    

    客户端代码:

    using System;
    using System.IO;
    using System.Net.Security;
    using System.Net.Sockets;
    using System.Security.Authentication;
    using System.Security.Cryptography.X509Certificates;
    using System.Text;
    
    class Client
    {
        static void Main(string[] args)
        {
            TcpClient client = new TcpClient("localhost", 8888);
            Console.WriteLine("Connected to server: " + client.Client.RemoteEndPoint.ToString());
    
            SslStream stream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate));
            X509Certificate2 cert = new X509Certificate2("client.pfx", "password");
            stream.AuthenticateAsClient("localhost", new X509Certificate2Collection(cert), SslProtocols.Tls, true);
    
            StreamReader reader = new StreamReader(stream, Encoding.UTF8);
            StreamWriter writer = new StreamWriter(stream, Encoding.UTF8) { AutoFlush = true };
    
            string message = reader.ReadLine();
    Console.WriteLine("Received message from server: " + message);
    
        writer.WriteLine("Hello from SSL client!");
        Console.WriteLine("Sent message to server: Hello from SSL client!");
    
        stream.Close();
        client.Close();
        Console.WriteLine("Disconnected from server.");
    }
    
    private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
        // In this example, we trust any certificate the server presents.
        // In production code, you should verify that the certificate is issued to the correct server and issued by a trusted certificate authority.
        return true;
    }
    }
    
    

    在此示例中,客户端连接到本地主机的端口 8888,并尝试建立 SSL 连接。客户端使用自签名的证书进行身份验证,并向服务器发送消息。服务器使用相同的自签名证书进行身份验证,并返回接收到的消息。客户端在控制台上输出服务器发送的消息,并发送一条消息作为响应。最后,客户端关闭 SSL 连接和 TcpClient 连接。

    在生产环境中,您应该使用由受信任的证书机构颁发的证书,以确保安全的 SSL 通信。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月27日
  • 已采纳回答 3月24日
  • 创建了问题 3月24日

悬赏问题

  • ¥30 电脑误删了手机的照片怎么恢复?
  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框
  • ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。
  • ¥20 学生成绩管理系统设计
  • ¥15 来一个cc穿盾脚本开发者
  • ¥15 CST2023安装报错
  • ¥15 使用diffusionbert生成文字 结果是PAD和UNK怎么办
  • ¥15 有人懂怎么做大模型的客服系统吗?卡住了卡住了
  • ¥20 firefly-rk3399上启动卡住了