结束连接异常

发布于 2024-08-06 10:05:01 字数 592 浏览 6 评论 0原文

我尝试使用 BeginConnect 连接到服务器,当我指定了不正确的 IpAddress 或端口时,我收到 SocketException。

问题是我的 try/catch 没有捕获异常:

private void OnConnect(IAsyncResult result)
{
    try
    {
        socket.EndConnect(result);

        status = Status.WaitAck;

        socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, onDataReady, null);
    }
    catch (Exception ex)
    {
        if (ConnectionError != null)
            ConnectionError(this, new OpenWebNetErrorEventArgs(ex));
    }
}

当我调用 socket.EndConnect 方法时,VS 报告异常并阻止程序...

我该如何处理?

谢谢 费德里科

I'm trying to connect to a server using BeginConnect and when I specified an incorrect IpAddress or Port I get a SocketException.

The problem is that my try/catch doesn't catch the Exception:

private void OnConnect(IAsyncResult result)
{
    try
    {
        socket.EndConnect(result);

        status = Status.WaitAck;

        socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, onDataReady, null);
    }
    catch (Exception ex)
    {
        if (ConnectionError != null)
            ConnectionError(this, new OpenWebNetErrorEventArgs(ex));
    }
}

When I call the socket.EndConnect method VS report me the exception and block the program...

How can I handle it?

Thanks
Federico

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀里藏娇 2024-08-13 10:05:01

您是否在调试器下运行该程序?如果是这样,您要么让 VS 在抛出任何异常时中断,要么在抛出 SocketException 时中断。

如果您解决了这个问题(取消选中该异常的“抛出”列),它应该允许您的 Catch 处理程序执行。

Are you running the program under debugger? If so, you either have VS to break on any exception being thrown, or break on the SocketException being thrown.

If you fix that (uncheck the "thrown" column for that exception) and it should allow your Catch handler to execute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文