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日

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来