I am trying to check the TCP connection to a localhost TCP server (ActiveMQ broker) using following code:
string host = "localhost";
int port = 61616;
using (TcpClient tcpClient = new TcpClient())
{
try
{
Task t = Task.Run(() => {
tcpClient.Connect(host, port);
});
Console.WriteLine("Connected.");
TimeSpan ts = TimeSpan.FromMilliseconds(150);
if (!t.Wait(ts))
{
Console.WriteLine("The timeout interval elapsed.");
Console.WriteLine("Could not connect to: {0}", port);// ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Port.ToString());
}
else
{
Console.WriteLine("Port {0} open.", port);
}
}
catch (UnauthorizedAccessException )
{
Console.WriteLine("Caught unauthorized access exception-await behavior");
}
catch (AggregateException )
{
Console.WriteLine("Caught aggregate exception-Task.Wait behavior");
}
I stopped the localhost server (ActiveMQ broker), and tried to run the above code. It threw System.AggregateException
. When I started the server and ran the code; it connects to the server.
According to the documentation of TcpClient.Connect
it says it will throw one of the following:
ArgumentNullException
ArgumentOutOfRangeException
SocketException
ObjectDisposedException
SecurityException
NotSupportedException
Why will it throw System.AggregateException
?