Esempio n. 1
0
        /// <summary>
        /// Resolves IP address. Throws the CanNotResolveHostName exception if resolving fails.
        /// </summary>
        /// <param name="url">Url to resolve.</param>
        /// <returns>Resolved IPAddress.</returns>
        public static IPAddress ResolveIPAddress(string url)
        {
            IPAddress ipAddress = null;

            try
            {
                if (url.Length > 0 && url[0] >= '0' && url[0] <= '9')
                {
                    ipAddress = IPAddress.Parse(url);
                }
            }
            catch
            {
            }

            if (ipAddress == null)
            {
                IPAddress[] addresses = Dns.GetHostEntry(url).AddressList;
                if (addresses.Length <= 0)
                {
                    throw GenuineExceptions.Get_Connect_CanNotResolveHostName(url);
                }
                ipAddress = addresses[0];
            }

            if (ipAddress == null)
            {
                throw GenuineExceptions.Get_Connect_CanNotResolveHostName(url);
            }
            return(ipAddress);
        }
Esempio n. 2
0
        /// <summary>
        /// Resolves the remote host IP address and establishes the connection.
        /// </summary>
        /// <param name="ignored">This parameter is ignored.</param>
        private void StartConnecting(object ignored)
        {
            try
            {
                lock (this._syncRoot)
                {
                    if (!this._continue)
                    {
                        return;
                    }
                }

                // resolve host name to IP address
                IPAddress ipAddress = GenuineUtility.ResolveIPAddress(hostName);

                if (ipAddress == null)
                {
                    throw GenuineExceptions.Get_Connect_CanNotResolveHostName(hostName);
                }

                // get the address of the remote host
                IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, this._portNumber);
                if (ipEndPoint == null)
                {
                    throw GenuineExceptions.Get_Connect_CanNotConnectToRemoteHost(this.hostName, "The IPEndPoint instance cannot be created.");
                }

                // create and setup a socket
                socket = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                LingerOption lingerOption = new LingerOption(true, 3);
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);

                if ((bool)this._parameterProvider[GenuineParameter.TcpDisableNagling])
                {
                    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 1);
                }

                int tcpReceiveBufferSize = (int)this._parameterProvider[GenuineParameter.TcpReceiveBufferSize];
                int tcpSendBufferSize    = (int)this._parameterProvider[GenuineParameter.TcpSendBufferSize];
                if (tcpReceiveBufferSize >= 0)
                {
                    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, tcpReceiveBufferSize);
                }
                if (tcpSendBufferSize >= 0)
                {
                    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, tcpSendBufferSize);
                }

                lock (this._syncRoot)
                {
                    if (!this._continue)
                    {
                        return;
                    }
                }

                // connect to the remote host
                socket.Connect(ipEndPoint);

                this.Completed.Set();
            }
            catch (Exception ex)
            {
                this.CancelConnecting(ex);
            }
        }