Esempio n. 1
0
        /// <summary>
        /// Connects to the specified host. If the hostname resolves to more than one IP address,
        /// all IP addresses will be tried for connection, until one of them connects.
        /// </summary>
        /// <param name="host">Host name or IP address.</param>
        /// <param name="port">Port to connect.</param>
        /// <param name="ssl">Specifies if connects to SSL end point.</param>
        /// <exception cref="ObjectDisposedException">Is raised when this object is disposed and this method is accessed.</exception>
        /// <exception cref="InvalidOperationException">Is raised when client is already connected.</exception>
        /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Is raised when port isnot in valid range.</exception>
        public void Connect(string host, int port, bool ssl)
        {
            ThrowIfObjectDisposed();
            ThrowIfConnected();

            AssertUtil.ArgumentNotEmpty(host, nameof(host));
            AssertUtil.AssertNetworkPort(port, nameof(port));

            var ips = NameResolver.GetHostAddresses(host);

            for (int i = 0; i < ips.Length; i++)
            {
                try
                {
                    Connect(null, new IPEndPoint(ips[i], port), ssl);
                    break;
                }
                catch (Exception ex)
                {
                    if (this.IsConnected)
                    {
                        throw ex;
                    }
                    // Connect failed for specified IP address,
                    // if there are some more IPs left, try next, otherwise forward exception.
                    else if (i == (ips.Length - 1))
                    {
                        throw ex;
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="hostName">Host name.</param>
        /// <param name="protocol">Bind protocol.</param>
        /// <param name="ip">IP address to listen.</param>
        /// <param name="port">Port to listen.</param>
        /// <param name="sslMode">Specifies SSL mode.</param>
        /// <param name="certificate">Certificate to use for SSL connections.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>ip</b> is null.</exception>
        /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception>
        public IPBindInfo(string hostName, BindProtocol protocol, IPAddress ip, int port, SslMode sslMode, X509Certificate2 certificate)
        {
            AssertUtil.ArgumentNotNull(ip, nameof(ip));
            AssertUtil.AssertNetworkPort(port, nameof(port));

            this.HostName = hostName;
            this.Protocol = protocol;
            this.EndPoint = new IPEndPoint(ip, port);

            this.SslMode     = sslMode;
            this.Certificate = certificate;
            if ((sslMode == SslMode.SSL || sslMode == SslMode.TLS) && certificate == null)
            {
                throw new ArgumentException($"SSL requested, but argument '{nameof(certificate)}' is not provided.");
            }
        }