原文网址: http://www.cnblogs.com/csdev
Networkcomms 是一款C# 语言编写的TCP/UDP通信框架 作者是英国人 以前是收费的 目前作者已经开源 许可是:Apache License v2
开源地址是:https://github.com/MarcFletcher/NetworkComms.Net
首先对证书这一块,本人也不是特别了解,只是把NetworkComms本身带的Demo重新进行了整理。如果写的不对,请批评指正。
首先
服务器端:
(1)生成证书:
public void CreateCert() { if (!File.Exists("testCertificate.pfx")) { CertificateDetails details = new CertificateDetails("CN=wwww", DateTime.Now, DateTime.Now.AddYears(1)); SSLTools.CreateSelfSignedCertificatePFX(details, "testCertificate.pfx"); } X509Certificate cert = new X509Certificate2("testCertificate.pfx"); sslOptions = new SSLOptions(cert, true); }
我们看到上面的代码中 证书作为参数赋值给 sslOptions
sslOptions将会赋值给监听器,代码如下:
ConnectionListenerBase listener = new TCPConnectionListener(customOptions, ApplicationLayerProtocolStatus.Enabled,sslOptions); Connection.StartListening(listener, thePoint, true);
运行服务器端,会生成一个
testCertificate.pfx文件,我们需要把这个文件拷贝给客户端 客户端相关代码:
X509Certificate cert = new X509Certificate2("testCertificate.pfx"); SSLOptions sslOptions = new SSLOptions(cert, true); connInfo = new ConnectionInfo(txtIP.Text, int.Parse(txtPort.Text)); newTcpConnection = TCPConnection.GetConnection(connInfo, customOptions, sslOptions); button1.Enabled = false; button1.Text = "连接成功";
这样没有证书的客户端就无法同服务器端建立连接了
源码:http://pan.baidu.com/s/1dEJEdNz
时间: 2024-08-24 16:47:28