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?

New Topic/Question
Reply



MultiQuote






|