结束连接异常
我尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否在调试器下运行该程序?如果是这样,您要么让 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.