This project is read-only.
1
Vote

Crash if blocked by firewall

description

Hi,

if you use the lib/source in an application blocked by the windows firewall, you get a crash.

To solve it, I changed following in WhoIs.Client.cs

Move new TcpClient ( server, port ); into try block, and check exception.
Add a return string.empty if you get an error.
Close the TCP connection at the end of try block.
In finally, check if tcpclient is null to close it
    public static string RawQuery ( string query, string server, int port = 43, Encoding encoding = null )
    {
        encoding = encoding ?? Encoding.ASCII;
        TcpClient tcpClient = null;

        try
        {
            tcpClient = new TcpClient ( server, port );

            using ( var s = tcpClient.GetStream () )
            {
                var queryBytes = Encoding.ASCII.GetBytes ( query + "\r\n" );
                s.Write ( queryBytes, 0, queryBytes.Length );

                const int buffSize = 8192;
                var readBuff = new byte[buffSize];
                var res = new StringBuilder ();
                var cbRead = default ( int );
                do
                {
                    cbRead = s.Read ( readBuff, 0, readBuff.Length );
                    res.Append ( encoding.GetString ( readBuff, 0, cbRead ) );
                } while ( cbRead > 0 );

                return res.ToString ();
            }
        }

        catch ( SocketException )
        {

        }

        catch ( ArgumentOutOfRangeException )
        {
            ;
        }

        catch ( ArgumentNullException )
        {
            ;
        }

        catch ( Exception )
        {
            ;
        }

        finally
        {
            if ( tcpClient != null )
                tcpClient.Close ();

        }

        return string.Empty;
    }
}

comments

jsakamoto wrote Jul 22, 2014 at 4:56 AM

Thank you for your reporting!

But, I'm feeling it is not good idea about "eating" exception...

How to determine what happens when method returned empty string only, without exception information?

Colud you please tell me your opinion about it?