求个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 通信。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 2无用
悬赏问题
- ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
- ¥15 如何在scanpy上做差异基因和通路富集?
- ¥20 关于#硬件工程#的问题,请各位专家解答!
- ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
- ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
- ¥30 截图中的mathematics程序转换成matlab
- ¥15 动力学代码报错,维度不匹配
- ¥15 Power query添加列问题
- ¥50 Kubernetes&Fission&Eleasticsearch
- ¥15 報錯:Person is not mapped,如何解決?