2

I'm working on project which contains socket programming service code, now my requirement is to configure ssl certificate with that code. My code is looks like below :-

 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);
        }
    }

I've tried to configure ssl certificate with TcpListener and SSLStream class but it didn't help and i don't have much idea about that :(

Thanks.

IPS
  • 417
  • 9
  • 22
  • Why this question down voted ? This is not a duplicate question, I'm looking for code which is similar to Tcp Listener :( – IPS Jun 29 '17 at 07:23
  • If you really wanted something _"which is similar to TcpListener"_, you'd just use `TcpListener`. But your title specifically says you _don't_ want `TcpListener`. In any case, it's the same either way. `SslStream` wraps any `Stream`; with `TcpListener` you get the stream from the `GetStream()` method, with `Socket` you pass the `Socket` to the `NetworkStream` constructor. Either way, this is already thoroughly covered on Stack Overflow. – Peter Duniho Jul 01 '17 at 03:53

0 Answers0