3 Replies - 331 Views - Last Post: 05 March 2011 - 07:26 PM Rate Topic: -----

#1 Crazy_Learner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 25-July 09

try and catch avoidance

Posted 05 March 2011 - 05:20 PM

I know the title may sound weird, but heres the problem:

i have this function that will go through a listing of ports, by specifying the start and ending port. in this the code then identifies if the port is open or closed. What i am trying to find out is if there is a method that could be used to avoid using the catch statement to say that the port is closed

        /// <summary>
        /// This Method Is What Performs The Recursive Action Of Detecting If Each Port Is Open Or Closed 
        /// </summary>
        /// <param name="StartingPort">The Port To Start The Check From</param>
        /// <param name="EndingPort">The Port To End The Check On</param>
        private void FindPorts(int StartingPort, int EndingPort)
        {
            // Recursively Go Through All Of The Ports And Determine If They Are Open Or Not. This Will Use A 
            // Error Catching Handling So That The User Is Not Notified Everytime One Is Found Closed. It Just 
            // Wont Add It To The List.
            for (int currentPort = StartingPort; currentPort <= EndingPort; currentPort++)
            {
                TcpClient scanner = new TcpClient();
                try
                {
                    scanner.Connect(Ports.Localhost, currentPort);
                    Console.WriteLine("Port " + currentPort + " Is Open");
                }
                catch (ArgumentNullException nullIpAddress)
                {

                }
                catch (ArgumentOutOfRangeException incorrectIpAddress)
                {

                }
                catch (SocketException socketClosed)
                {
                    Console.WriteLine("Port " + currentPort + " Closed");
                }
                catch (ObjectDisposedException sannerObjectBad)
                {

                }
                finally
                {
                    scanner.Close();
                }
            }
        }




i am trying to make it to where an exception has to be thrown in order to make this portion of the code function. is there any way i can test this before the connection string since that is the statement that is throwing the exception?

Is This A Good Question/Topic? 0
  • +

Replies To: try and catch avoidance

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8909
  • View blog
  • Posts: 33,398
  • Joined: 12-June 08

Re: try and catch avoidance

Posted 05 March 2011 - 05:29 PM

Have you looked into the System.Net.networkinformation library? Specifically the TCPconnectioninformation object?

http://msdn.microsof...nformation.aspx


http://msdn.microsof...nformation.aspx

It looks like you can test the state with that.
Was This Post Helpful? 0
  • +
  • -

#3 Crazy_Learner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 25-July 09

Re: try and catch avoidance

Posted 05 March 2011 - 05:32 PM

no i have not, but thank you for point that out to me. i will look at the documentation. had not realized it was there
Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: try and catch avoidance

Posted 05 March 2011 - 07:26 PM

I believe what you want is IPGlobalProperties.GetActiveTcpListeners
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1