简体   繁体   English

为什么 TcpClient.Connect() 抛出 System.AggregateException

[英]Why is TcpClient.Connect() throwing System.AggregateException

I am trying to check the TCP connection to a localhost TCP server (ActiveMQ broker) using following code:我正在尝试使用以下代码检查与本地主机 TCP 服务器(ActiveMQ 代理)的 TCP 连接:

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.我停止了本地主机服务器(ActiveMQ 代理),并尝试运行上面的代码。 It threw System.AggregateException .它抛出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:根据TcpClient.Connect的文档,它说它将抛出以下之一:

  • ArgumentNullException
  • ArgumentOutOfRangeException
  • SocketException
  • ObjectDisposedException
  • SecurityException
  • NotSupportedException

Why will it throw System.AggregateException ?为什么会抛出System.AggregateException

This这个

Task t = Task.Run(() => {
    tcpClient.Connect(host, port);
});

wraps your .Connect() call.包装您的.Connect()电话。 And Task.Run() always throws AggregateException with the real exception inside it.并且Task.Run()总是抛出AggregateException ,其中包含真正的异常。 In order to fix this either inspect the exception or even better use the asynchronous variant of .Connect() :为了解决这个问题,要么检查异常,要么更好地使用.Connect()的异步变体:

Task t = tcpClient.ConnectAsync(host, port);

instead.反而。

What i did finally is:我最后做的是:

tcpClient.ReceiveTimeout = 5000;
 tcpClient.SendTimeout = 5000;
 tcpClient.Connect(host, port);
 catch (SocketException) {
                Console.WriteLine("Could not connect to: {0}", port);
                Console.WriteLine("Socket exception. Check host address and port.");
            }

It seems to be working.它似乎在工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 异常被捕获为System.AggregateException - Exception catched as System.AggregateException 为什么我的try / catch块没有捕获System.AggregateException? - Why isn't my try/catch block catching System.AggregateException? System.Threading.Tasks.TaskExceptionHolder.Finalize()上的System.AggregateException - System.AggregateException at System.Threading.Tasks.TaskExceptionHolder.Finalize() 如何修复 mscorlib.dll 中发生“'System.AggregateException'” - How to fix “'System.AggregateException' occurred in mscorlib.dll” System.AggregateException:发生一个或多个错误。 ---> System.Web.Services.Protocols.SoapException - System.AggregateException: One or more errors occurred. ---> System.Web.Services.Protocols.SoapException 尝试在 ASP.NET CORE 3.1 中注册服务时出现 System.AggregateException 异常 - System.AggregateException exception while attempting to register a service in ASP.NET CORE 3.1 mscorlib.dll 中出现“System.AggregateException”类型的异常,但未在用户代码中处理 - An exception of type 'System.AggregateException' occurred in mscorlib.dll but was not handled in user code 在我自己的代码中抛出AggregateException - Throwing an AggregateException in my own code 为什么这段代码会抛出'System.ArgumentOutOfRangeException'(SQlite)? - Why is this code throwing 'System.ArgumentOutOfRangeException' (SQlite)? System.dll中TCPClient.Close的ObjectDisposedException? - ObjectDisposedException for TCPClient.Close in System.dll?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM