使用SslStream和Socket类而不是Tcp监听器类

时间:2017-06-29 05:37:38

标签: c# sockets c#-4.0 c#-3.0 c#-2.0

我正在处理包含套接字编程服务代码的项目,现在我的要求是使用该代码配置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证书,但它没有帮助,我对此并不了解:(

感谢。

0 个答案:

没有答案