如何中止套接字的

如何中止套接字的

本文介绍了如何中止套接字的 BeginReceive()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当然,如果没有数据,BeginReceive() 将永远不会结束.MSDN 建议调用 Close()将中止 BeginReceive().

Naturally, BeginReceive() will never end if there's no data.MSDN suggests that calling Close() would abort BeginReceive().

但是,在套接字上调用 Close() 也会对其执行 Dispose(),如 这个很好的答案,因此 EndReceive() 会抛出一个异常,因为该对象已经被释放(确实如此!).

However, calling Close() on the socket also performs a Dispose() on it, as figured out in this great answer, and consequently EndReceive() would throw an exception because the object is already disposed (and it does!).

我应该如何进行?

推荐答案

这似乎是(非常愚蠢的)设计的.您必须在代码中抛出并捕获此异常.

It seems like this is by (the very dumb) design. You must have this exception thrown and caught in your code.

MSDN 看起来确实对此保持沉默,但是如果您查看另一个异步套接字方法的文档,BeginConnect(),这是我们发现的:

MSDN looks silent about it indeed, but if you look at the documentation of another asynchronous socket method, BeginConnect(), here's what we find:

取消挂起的呼叫BeginConnect() 方法,关闭插座.当 Close() 方法是在异步操作时调用正在进行中,提供了回调到 BeginConnect() 方法是叫.随后调用EndConnect(IAsyncResult) 方法将抛出 ObjectDisposedException 到表明操作已经取消了.

如果对于 BeginConnect 来说这是正确的做法,那么对于 BeginReceive 来说可能也是如此.就 Microsoft 的异步 API 而言,这当然是一个糟糕的设计,因为让用户必须将异常作为正常流程的一部分抛出和捕获会惹恼调试器.你真的没有办法等待"直到操作完成,因为 Close() 是首先完成它的.

If it is the proper way of doing for BeginConnect, it is probably so for BeginReceive as well. This is certainly a poor design on the part of Microsoft's async API, because making the user necessarily throw and catch exception as a part of a normal flow would annoy the debugger. You have really no way to "wait" until the operation is completed, because Close() is what completes it in the first place.

这篇关于如何中止套接字的 BeginReceive()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 05:20