Esempio n. 1
0
        /// <summary>
        /// Connect to remote server
        /// </summary>
        public void Connect()
        {
            this.socket = new Socket(this.RemoteIpAddress.GetAddressFamily(), SocketType.Stream, ProtocolType.Tcp);
            // try connection to the broker
            this.socket.Connect(new IPEndPoint(this.RemoteIpAddress, this.RemotePort));

            // secure channel requested
            if (secure)
            {
                // create SSL stream
                this.netStream = new NetworkStream(this.socket);
                this.sslStream = new SslStream(this.netStream, false, this.userCertificateValidationCallback, this.userCertificateSelectionCallback);

                // server authentication (SSL/TLS handshake)
                X509CertificateCollection clientCertificates = null;
                // check if there is a client certificate to add to the collection, otherwise it's null (as empty)
                if (this.clientCert != null)
                {
                    clientCertificates = new X509CertificateCollection(new X509Certificate[] { this.clientCert });
                }

                this.sslStream.AuthenticateAsClient(this.RemoteHostName,
                                                    clientCertificates,
                                                    MqttSslUtility.ToSslPlatformEnum(this.sslProtocol),
                                                    false);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Accept connection from a remote client
        /// </summary>
        public void Accept()
        {
            // secure channel requested
            if (secure)
            {
                this.netStream = new NetworkStream(this.socket);
                this.sslStream = new SslStream(this.netStream, false, this.userCertificateValidationCallback, this.userCertificateSelectionCallback);

                this.sslStream.AuthenticateAsServer(this.serverCert, false, MqttSslUtility.ToSslPlatformEnum(this.sslProtocol), false);
            }
        }