求个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 询问MYSQL查询SQLSERVER数据表并比较差异后,更新MYSQL的数据表
- ¥15 关于#前端#的问题,请各位专家解答!
- ¥15 最小生成树问题 Prim算法和Kruskal算法
- ¥25 医院住院病人呼叫器设计
- ¥15 不想和现在的团队合作了,怎么避免他们对程序动手脚
- ¥30 c++类和数组实验代码
- ¥20 C语言字符串不区分大小写字典排序相关问题
- ¥15 关于#python#的问题:我希望通过逆向技术爬取1688搜索页下滑加载的数据
- ¥15 关于Linux的终端里,模拟实现一个带口令保护的屏保程序遇到的输入输出的问题!(语言-c语言)
- ¥30 请问,这个嵌入式Linux系统怎么分析,crc检验区域在哪