예제 #1
0
        /// <summary>
        /// Creates a socket and connects it to the specified port on
        /// the specified host. </summary>
        /// <param name="host"> the specified host </param>
        /// <param name="port"> the specified port </param>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: protected void connect(String host, int port) throws UnknownHostException, java.io.IOException
        protected internal override void Connect(String host, int port)
        {
            bool connected = false;

            try
            {
                InetAddress address = InetAddress.GetByName(host);
                this.Port_Renamed = port;
                this.Address      = address;

                ConnectToAddress(address, port, Timeout_Renamed);
                connected = true;
            }
            finally
            {
                if (!connected)
                {
                    try
                    {
                        Close();
                    }
                    catch (IOException)
                    {
                        /* Do nothing. If connect threw an exception then
                         * it will be passed up the call stack */
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Get the IP address of our host. An empty host field or a DNS failure
        /// will result in a null return.
        /// </summary>
        /// <param name="u"> a URL object </param>
        /// <returns> an {@code InetAddress} representing the host
        /// IP address.
        /// @since 1.3 </returns>
        protected internal virtual InetAddress GetHostAddress(URL u)
        {
            lock (this)
            {
                if (u.HostAddress != null)
                {
                    return(u.HostAddress);
                }

                String host = u.Host;
                if (host == null || host.Equals(""))
                {
                    return(null);
                }
                else
                {
                    try
                    {
                        u.HostAddress = InetAddress.GetByName(host);
                    }
                    catch (UnknownHostException)
                    {
                        return(null);
                    }
                    catch (SecurityException)
                    {
                        return(null);
                    }
                }
                return(u.HostAddress);
            }
        }
예제 #3
0
        ///
        /// <summary>
        /// Creates a socket address from a hostname and a port number.
        /// <para>
        /// An attempt will be made to resolve the hostname into an InetAddress.
        /// If that attempt fails, the address will be flagged as <I>unresolved</I>.
        /// </para>
        /// <para>
        /// If there is a security manager, its {@code checkConnect} method
        /// is called with the host name as its argument to check the permission
        /// to resolve it. This could result in a SecurityException.
        /// <P>
        /// A valid port value is between 0 and 65535.
        /// A port number of {@code zero} will let the system pick up an
        /// ephemeral port in a {@code bind} operation.
        /// <P>
        /// </para>
        /// </summary>
        /// <param name="hostname"> the Host name </param>
        /// <param name="port">    The port number </param>
        /// <exception cref="IllegalArgumentException"> if the port parameter is outside the range
        /// of valid port values, or if the hostname parameter is <TT>null</TT>. </exception>
        /// <exception cref="SecurityException"> if a security manager is present and
        ///                           permission to resolve the host name is
        ///                           denied. </exception>
        /// <seealso cref=     #isUnresolved() </seealso>
        public InetSocketAddress(String hostname, int port)
        {
            CheckHost(hostname);
            InetAddress addr = null;
            String      host = null;

            try
            {
                addr = InetAddress.GetByName(hostname);
            }
            catch (UnknownHostException)
            {
                host = hostname;
            }
            Holder = new InetSocketAddressHolder(host, addr, CheckPort(port));
        }