我正在处理包含套接字编程服务代码的项目,现在我的要求是使用该代码配置ssl证书。 我的代码如下所示: -
void Listen(int port)
{
try
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
IPEndPoint ep = new IPEndPoint(IPAddress.Any, port);
socket.Bind(ep);
socket.Listen(4);
//NetworkStream ns = new NetworkStream(socket, false);
//SslStream sslStream = new SslStream(ns,true);
//X509Certificate2 certificate = new X509Certificate2("C:\\Program Files\\Nimble Streamer\\conf\\my_certificate.crt", "Xdg1AjUWrZkLb2Z7iM8I");
//sslStream.AuthenticateAsServer(certificate, true, System.Security.Authentication.SslProtocols.Tls, true);
Logger.Log(Logger.Milestone, "server listening on port {0}", port);
// open sockets for sending RTP data over UDP
rtp_sockets = new Socket[2];
for (int i = 0; i < 2; i++)
{
rtp_sockets[i] = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
rtp_sockets[i].SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
ep = new IPEndPoint(IPAddress.Any, server_port + i);
rtp_sockets[i].Bind(ep);
}
// begin accepting new connections
evt.AcceptSocket = null;
evt.Completed += new EventHandler<SocketAsyncEventArgs>(IOComplete);
bool pending = socket.AcceptAsync(evt);
if (!pending)
{
IOComplete(null, evt);
}
}
catch (Exception ex)
{
Logger.Log(ex);
}
}
我尝试使用TcpListener和SSLStream类配置ssl证书,但它没有帮助,我对此并不了解:(
感谢。