简体   繁体   English

正确处理Windows CE上的网络超时

[英]Properly handling network timeouts on Windows CE

I'm trying perform a relatively basic TCP socket send/receive operation on Windows CE using the .NET Compact Framework. 我正在尝试使用.NET Compact Framework在Windows CE上执行相对基本的TCP套接字发送/接收操作。 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. 在完整的框架上,我只需在Socket对象上设置ReceiveTimeoutSendTimeout属性即可。 Unfortunately, setting these properties on the compact framework immediately results in a SocketException about using an unsupported socket option. 不幸的是,在紧凑框架上设置这些属性会立即导致使用不受支持的套接字选项的SocketException。

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.

So it doesn't look like Windows CE supports timeouts. 所以它看起来不像Windows CE支持超时。 A timeout will eventually occur on an unresponsive connection but it seems to take about a minute (must be hardcoded somewhere in WinCE). 在无响应的连接上最终会发生超时,但似乎需要大约一分钟(必须在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) . 我的第一个想法是使用异步IO,它允许我WaitOne(timeout) However, this won't stop the asynchronous thread that will be stuck on EndSend() or EndReceive() . 但是,这不会停止将停留在EndSend()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. 我能想到解决这个问题的唯一方法是中止异步线程,但这似乎是一个非常糟糕的主意,我想避免它。

So what is the correct way to handle this? 那么处理这个问题的正确方法是什么? There must be a simple way since other applications (eg 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. 必须有一个简单的方法,因为其他应用程序(例如WinCE上的IE)似乎没有任何问题超时或取消待处理的网络操作,他们似乎也能够顺利关闭。

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. 如果等待超时,那么简单地关闭(或处理)套接字将取消另一个线程中的阻塞读/写,从而导致该线程上的优雅(在Send()/ Receive()调用上抛出异常)。 Hopefully this will be helpful to someone else... 希望这对其他人有帮助......

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM