阻止Select()不起作用 [英] Blocking Select() does not work

查看:91
本文介绍了阻止Select()不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在套接字上使用阻塞的Select()调用

超时值为-1。我希望这个电话会无限期阻止
,但事实并非如此。当我使用Poll()时,

a超时-1工作正常并且无限期地阻塞。


净效果是我不能写更多选择

比一个文件描述符,如果我想阻止。 (使用

超时值> = 0,Select()和Poll()都可正常工作。)


要重现,请运行以下代码并telnet从另一个窗口到12345的127.0.0.1

。 Select()版本

立即返回,即使它不应该,而

Poll()版本正确阻止并返回一次

你点击了telnet窗口中的任意一个键。


干杯,


Michi。


#定义SHOW_BUG


使用System;

使用System.Collections;

使用System.Net;

使用System.Net.Sockets;


类服务器

{

static void Main(string [] args)

{

Socket s = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

s.Bind(new IPEndPoint(IPAddress) .Parse(" 127.0.0.1"),12345));

s.Listen(1);

Socket c = s.Accept();

#if SHOW_BUG

ArrayList readList = new ArrayList();

readList.Add(c);

Socket。选择(readList,null,null,-1);

Console.WriteLine(" Select return,可读描述符数量= + readList.Count);

#else

bool rc = c.Poll(-1,SelectMode.SelectRead);

Console.WriteLine (Poll返回+ rc);

#endif

}

}

解决方案

Michi Henning< mi *** @ zeroc.com>写道:

我正在使用阻塞的Select()调用套接字,其超时值为-1。我希望这个电话无限期阻止,但事实并非如此。当我使用Poll()代替时,
-1的超时工作正常并且无限期地阻塞。




为什么你会期望它无限期地阻塞?我不能在

中看到任何文件来表明它应该 - 而民意调查

文件*确实*说负值会导致潜在的/>
无限期等待。


-

Jon Skeet - < sk *** @ pobox.com>
< a rel =nofollowhref =http://www.pobox.com/~skeettarget =_ blank> http://www.pobox.com/~skeet

如果回复该组,请不要给我发邮件


" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...

Michi Henning< mi *** @ zeroc.com>写道:

我正在使用阻塞的Select()调用套接字,其超时值为-1。我希望这个电话无限期阻止,但事实并非如此。当我使用Poll()代替时,
-1的超时工作正常并无限期地阻塞。



为什么你会期望它无限期地阻塞?我无法在文档中看到任何暗示它应该的内容 - 而民意调查
文档*确实*说负值会导致潜在的无限期等待。




对,这正是文档所说的。但本机select()(在SDK中)

在使用否定超时调用时无限期阻塞。而且,如果文档中说的是真正的预期,那么根据一次就多个套接字进行阻塞选择根本就没有办法

。这显然是一个错误 - 如果我有多个数据源我想要监控

的活动,除非我这样做,否则我不能这样做忙碌的等待。


如果Poll()无限期地阻止负超时,那么Select()

(这正是我''时发生的事情使用SDK代替

.NET frameowork)。


干杯,


Michi。


-

Michi Henning电话:+61 4 1118-2700

ZeroC,Inc。 http://www.zeroc.com


Michi Henning < MI *** @ triodia.com>在新闻中写道:OZeRL


Hi,

I''m using a blocking Select() call on a socket with
a timeout value of -1. I''d expect the call to block
indefinitely, but it doesn''t. When I use Poll() instead,
a timeout of -1 works fine and blocks indefinitely.

The net effect is that I cannot write a select on more
than one file descripter if I want to block. (With
timeout values >= 0, both Select() and Poll() work fine.)

To reproduce, run the code below and telnet to 127.0.0.1
at port 12345 from another window. The Select() version
returns immediately even though it shouldn''t, whereas
the Poll() version correctly blocks and returns once
you hit any key in the telnet window.

Cheers,

Michi.

#define SHOW_BUG

using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;

class Server
{
static void Main(string[] args)
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12345));
s.Listen(1);
Socket c = s.Accept();
#if SHOW_BUG
ArrayList readList = new ArrayList();
readList.Add(c);
Socket.Select(readList, null, null, -1);
Console.WriteLine("Select returned, number of readable descriptors = " + readList.Count);
#else
bool rc = c.Poll(-1, SelectMode.SelectRead);
Console.WriteLine("Poll returned " + rc);
#endif
}
}


解决方案

Michi Henning <mi***@zeroc.com> wrote:

I''m using a blocking Select() call on a socket with
a timeout value of -1. I''d expect the call to block
indefinitely, but it doesn''t. When I use Poll() instead,
a timeout of -1 works fine and blocks indefinitely.



Why would you expect it to block indefinitely? I can''t see anything in
the documentation to suggest that it should - whereas the Poll
documentation *does* say that a negative value will cause a potentially
indefinite wait.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...

Michi Henning <mi***@zeroc.com> wrote:

I''m using a blocking Select() call on a socket with
a timeout value of -1. I''d expect the call to block
indefinitely, but it doesn''t. When I use Poll() instead,
a timeout of -1 works fine and blocks indefinitely.



Why would you expect it to block indefinitely? I can''t see anything in
the documentation to suggest that it should - whereas the Poll
documentation *does* say that a negative value will cause a potentially
indefinite wait.



Right, that''s exactly what the doc says. But native select() (in the SDK)
blocks indefinitely when called with a negative timeout. And, if what
the documentation says is really as intended, there is simply no way
to do a blocking select on more than a single socket at a time. That''s
clearly a bug -- if I have multiple sources of data that I want to monitor
for activity, I can''t do that unless I do a busy wait.

If Poll() blocks indefinitely with a negative timeout, so should Select()
(which is exactly what happens when I''m using the SDK instead of
the .NET frameowork).

Cheers,

Michi.

--
Michi Henning Ph: +61 4 1118-2700
ZeroC, Inc. http://www.zeroc.com


"Michi Henning" <mi***@triodia.com> wrote in news:OZeRL


这篇关于阻止Select()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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