插座ReceiveTimeout [英] Socket ReceiveTimeout

查看:193
本文介绍了插座ReceiveTimeout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已指定 ReceiveTimout 40毫秒。但是这需要500ms以上为接收超时。我用秒表来计算timetaken。



中的代码如下所示:

 插座的TCPSocket =新的Socket(AddressFamily.InterNetwork,SocketType.Stream,
ProtocolType.Tcp);
TCPSocket.ReceiveTimeout = 40;


{
TCPSocket.Receive(缓冲);

}赶上(SocketException五){}


解决方案

您可以同步查询与你希望的任何超时插座上。如果民意测验()收益真正,你可以肯定,你可以对通话接收(),将不会阻止

 插座S。 
// ...
//投票的插座接待了10毫秒超时。
如果(s.Poll(10000,SelectMode.SelectRead))
{
s.Receive(); //此调用不会阻塞
}
,否则
{
//超时
}

我建议你阅读非阻塞插座使用Stevens的UNIX网络编程第6和第16进行更深入的信息。虽然这本书在其名称中具有UNIX,整体插座架构本质上是在UNIX和Windows(和.net)


相同

I have specified the ReceiveTimout as 40 ms. But it takes more than 500ms for the receive to timeout. I am using a Stopwatch to compute the timetaken.

The code is shown below.

Socket TCPSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
                              ProtocolType.Tcp);
TCPSocket.ReceiveTimeout = 40;

try
{  
    TCPSocket.Receive(Buffer);  

}  catch(SocketException e)  {  }

解决方案

You can synchronously poll on the socket with any timeout you wish. If Poll() returns true, you can be certain that you can make a call to Receive() that won't block.

Socket s;
// ...
// Poll the socket for reception with a 10 ms timeout.
if (s.Poll(10000, SelectMode.SelectRead))
{
    s.Receive(); // This call will not block
}
else
{
    // Timed out
}

I recommend you read Stevens' UNIX Network Programming chapters 6 and 16 for more in-depth information on non-blocking socket usage. Even though the book has UNIX in its name, the overall sockets architecture is essentially the same in UNIX and Windows (and .net)

这篇关于插座ReceiveTimeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆