妥善处理在Windows CE网络超时 [英] Properly handling network timeouts on Windows CE
问题描述
我试图执行一个比较基本的TCP套接字发送/使用.NET Compact Framework的Windows CE的接收操作。我试图设置超时值,以便读取速度较慢的连接超时/写永远堵来代替。在完整的框架,我可以简单地将 ReceiveTimeout
和的SendTimeout
上的属性插座
对象。不幸的是,紧凑的框架设置这些属性立即导致有关使用不受支持的套接字选项SocketException。
I'm trying perform a relatively basic TCP socket send/receive operation on Windows CE using the .NET Compact Framework. I'm trying set the timeout value so that reads/writes on a slow connection timeout instead of blocking forever. On the full framework I'm able to simply set the ReceiveTimeout
and SendTimeout
properties on the Socket
object. Unfortunately, setting these properties on the compact framework immediately results in a SocketException about using an unsupported socket option.
进一步深挖一点后,我碰到的这个页,上面写着以下内容:
After digging a little further, I came across this page that says the following:
The following table shows BSD options not supported for setsockopt:
Value Type Description
SO_ACCEPTCONN BOOL Sets socket listening.
SO_RCVLOWAT int Sets recv low watermark.
SO_RCVTIMEO int Sets time-out for recv.
SO_SNDLOWAT int Sets send low watermark.
SO_SNDTIMEO int Sets time-out value for send.
SO_TYPE int Sets socket type.
因此,它看起来并不像Windows CE的支持超时。超时最终会出现无响应的连接上,但它似乎大约需要一分钟(必须在WinCE的地方硬编码)。所以现在我想弄清楚如何手动实现这一点。我首先想到的是用异步IO,让我 WaitOne的(超时)
。但是,这不会停止异步线程将在 EndSend被卡住()
或 EndReceive()
。所以,即使我可以超时我的主线程仍然会有线程,直到硬编码的超时被击中左右徘徊。在此期间,我的应用程序将无法正常关闭。我能想到的解决此问题的工作的唯一办法是中止异步线程,但是这似乎是一个非常糟糕的主意,我想,以避免它。
So it doesn't look like Windows CE supports timeouts. A timeout will eventually occur on an unresponsive connection but it seems to take about a minute (must be hardcoded somewhere in WinCE). So now I'm trying to figure out how to implement this manually. My first thought is to use asynchronous IO which allows me to WaitOne(timeout)
. However, this won't stop the asynchronous thread that will be stuck on EndSend()
or EndReceive()
. So even if I can timeout my main thread there will still be threads lingering around until the hardcoded timeout is hit. During this time my application won't shut down properly. The only way I can think of working around this problem would be to abort the asynchronous thread but this seems like a very bad idea and would I like to avoid it.
所以,什么是处理这个问题的正确方法?必须有一个简单的方法,因为其他应用程序似乎(如IE的WinCE)不会有任何麻烦超时或取消待定的网络操作,他们似乎能够不闹也关机。
So what is the correct way to handle this? There must be a simple way since other applications (e.g. IE on WinCE) don't seem to have any trouble timing out or canceling pending network operations and they seem to be able to shutdown without trouble as well.
推荐答案
我发现了一个干净的方式来做到这一点。基本上我做的发送和一个单独的线程接收,然后等待一个事件(手动或自动复位事件应该工作)。如果等待超时,然后简单地插座的关闭(或处置)将取消在其他线程阻塞读/写引起优雅(抛出一个异常的发送()/接收()调用)退出该线程。希望这将有助于别人...
I found a clean way to do this. Basically I am doing the sending and receiving in a separate thread and then waiting on an event (manual or auto reset event should work). If the wait times out then simply closing (or disposing) of the socket will cancel the blocking read/write in the other thread causing a graceful (an exception is thrown on the Send()/Receive() call) exit on that thread. Hopefully this will be helpful to someone else...
这篇关于妥善处理在Windows CE网络超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!