异步插座&活动连接 [英] Async socket & active connections

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

问题描述




为了创建一个完整的网络浏览器我发现自己

在Socket类中编写一个包装器尝试使它完全异步,支持超时和一些调度机制。

我使用非阻塞方法,使用调用''poll''

支持异步机制 - 而不是''开始''和''结束''

函数。我已经发现连接没有设置

" isconnected"变量正确(抛出SocketException:非

阻塞有这种效果......) - 但似乎不是问题

因为轮询,读写工作正常。


为了测量爬虫的性能,我开始perfmon.msc

并添加了活动连接。来自对象TCP的项目。在

之后我发现这个性能计数器的数量达到了超过

300K连接(!),足以开始担心...

我的抓取工具旨在同时支持大约200个连接。

" netstat -an"并不支持这一发现,但确实显示了数百个

连接,这些连接位于CLOSE_WAIT,FIN_WAIT_2和FIN_WAIT中。或者另一个

关闭状态。


主机完成后,我尝试断开TCP / IP连接。

我'尝试'shutdown(both)',(async)" disconnect"

和'close(0)"的组合。 - 没有任何组合似乎具有所需的

效果。关闭应用程序后,将删除所有连接(包括

CLOSE_WAIT连接)。 FIN_WAIT_2连接

永远逗留...


也许有人知道这个问题的解决方案吗?


问候,


Stefan de Bruijn。

解决方案

3月25日21:48, " atlaste" < atla ... @ gmail.comwrote:





试图创造一个全面的webcrawler我找到了自己

在Socket类周围编写一个包装器,试图使它完全异步,支持超时和一些调度机制。

我使用非阻塞方法,使用调用''poll''

支持异步机制 - 而不是''begin''和''end' '

功能。我已经发现连接没有设置

" isconnected"变量正确(抛出SocketException:非

阻塞有这种效果......) - 但似乎不是问题

因为轮询,读写工作正常。


为了测量爬虫的性能,我开始perfmon.msc

并添加了活动连接。来自对象TCP的项目。在

之后我发现这个性能计数器的数量达到了超过

300K连接(!),足以开始担心...

我的抓取工具旨在同时支持大约200个连接。

" netstat -an"并不支持这一发现,但确实显示了数百个

连接,这些连接位于CLOSE_WAIT,FIN_WAIT_2和FIN_WAIT中。或者另一个

关闭状态。


主机完成后,我尝试断开TCP / IP连接。

我'尝试'shutdown(both)',(async)" disconnect"

和'close(0)"的组合。 - 没有任何组合似乎具有所需的

效果。关闭应用程序后,将删除所有连接(包括

CLOSE_WAIT连接)。 FIN_WAIT_2连接

永远逗留...


也许有人知道这个问题的解决方案吗?


问候,


Stefan de Bruijn。



只是一个猜测,你可以在keepalive设置为0的情况下尝试吗?


类似于s是Socket的地方:


s.SetSocketOption(System.Net.Sockets.SocketOptionL evel.IP,

System.Net.Sockets.SocketOptionName.KeepAlive,0);


感谢您的反应。


这是我对TCP / IP的理解keepalive有不同的含义

(参见 http://msdn.microsoft.com/library/de...et_options.asp

http://msdn2.microsoft.com/en-us/lib...ptionname。 aspx

)比你的建议。将其设置为true应该在2小时后关闭

连接或类似的事情。 (因为两个小时

听起来像是一辈子给我,我可以看到名字keepalive

来源:-)


所以虽然把它设置为真的确实是个好主意 -

并没有真正解决我的问题我很害怕。

也许有人有另一个建议?


干杯,

Stefan de Bruijn。

3月26日,11日:上午44点,DeveloperX < nntp ... @ operamail.comwrote:


3月25日21:48,atlaste < atla ... @ gmail.comwrote:




In尝试创建一个完整的webcrawler我发现自己

在Socket类周围编写一个包装器,试图使它完全异步,支持超时和支持超时一些调度机制。

我使用非阻塞方法,使用调用''poll'来支持异步机制 - 而不是''开始''和''结束''

函数。我已经发现连接没有设置

" isconnected"变量正确(抛出SocketException:非

阻塞有这种效果......) - 但似乎不是问题

因为轮询,读写工作得很好。


为了衡量抓取工具的性能,我开始perfmon.msc

并添加了有效连接"来自对象TCP的项目。在

之后我发现这个性能计数器的数量达到了超过

300K连接(!),足以开始担心...


我的抓取工具旨在同时支持大约200个连接。

" netstat -an"并不支持这一发现,但确实显示了数百个

连接,这些连接位于CLOSE_WAIT,FIN_WAIT_2和FIN_WAIT中。或另一个

关闭状态。


主机完成后,我尝试断开TCP / IP连接。

我尝试过组合shutdown(both),(async)" disconnect"

和close(0)" - 没有任何组合似乎具有所需的

效果。关闭应用程序后,将删除所有连接(包括

CLOSE_WAIT连接)。 FIN_WAIT_2连接

永远逗留...


也许有人知道这个问题的解决方案?


Greetings,


Stefan de Bruijn。



只是一个猜测,你可以尝试将keepalive设置为0吗?


类似于s是Socket的地方:


s.SetSocketOption(System.Net.Sockets.SocketOptionL evel.IP,

System.Net.Sockets.SocketOptionName.KeepAlive,0);



3月26日12:43,atlaste < atla ... @ gmail.comwrote:


感谢您的反应。


这是我对TCP的理解/ IP keepalive有不同的含义

(参见http://msdn.microsoft.com/library/default.asp?url = / library / en-us / wins ...

andhttp://msdn2.microsoft.com/en-us/library/system.net.sockets.socketopt ...

)比你的建议。将其设置为true应该在2小时后关闭

连接或类似的事情。 (因为两个小时

听起来像是一辈子给我,我可以看到名字keepalive

来源:-)


所以虽然把它设置为真的确实是个好主意 -

并没有真正解决我的问题我很害怕。

也许有人有另一个建议?


干杯,

Stefan de Bruijn。


开3月26日上午11:44,DeveloperX < nntp ... @ operamail.comwrote:


3月25日21:48,atlaste < atla ... @ gmail.comwrote:




为了创建一个完整的网络浏览器我发现自己已经找到了自己

写一个包装器在Socket类周围尝试使它完全异步,支持超时和一些调度机制。

我使用非阻塞方法,使用调用''poll''

支持异步机制 - 而不是''开始''和''结束''

函数。我已经发现连接没有设置

" isconnected"变量正确(抛出SocketException:非

阻塞有这种效果......) - 但似乎不是问题

因为轮询,读写工作得很好。


为了测量爬虫的性能,我开始使用perfmon.msc

并添加了活动连接来自对象TCP的项目。在

之后我发现这个性能计数器的数量达到了超过

300K连接(!),足以开始担心...


我的抓取工具旨在同时支持大约200个连接。

" netstat -an"并不支持这一发现,但确实显示了数百个

连接,这些连接位于CLOSE_WAIT,FIN_WAIT_2和FIN_WAIT中。或另一个

关闭状态。


主机完成后,我尝试断开TCP / IP连接。 />
我尝试过组合shutdown(both),(async)" disconnect"

和close(0) - 没有任何组合似乎具有所需的

效果。关闭应用程序后,将删除所有连接(包括

CLOSE_WAIT连接)。 FIN_WAIT_2连接

永远逗留...


也许某人知道这个问题的解决方案吗?


Greetings,


Stefan de Bruijn。


只是一个猜测,你可以尝试将keepalive设置为0吗?


类似于s是Socket的地方:


s。 SetSocketOption(System.Net.Sockets.SocketOptionL evel.IP,

System.Net.Sockets.SocketOptionName.KeepAlive,0); - 隐藏引用的文本 -



- 显示引用的文字 -



啊,是的,我知道,如何更改灵儿选项?


基本上这个建议是因为我读到的一些内容

Apache更改关闭连接的方法也变成了Linger_Close

并且它给人们带来了很多FIN_WAIT_2问题。


再次,只是一个想法:)


Hi,

In an attempt to create a full-blown webcrawler I''ve found myself
writing a wrapper around the Socket class in an attempt to make it
completely async, supporting timeouts and some scheduling mechanisms.
I use a non-blocking approach for this, using the call to ''poll'' to
support the async mechanism - rather than the ''begin'' and ''end''
functions. I already found that connecting doesn''t set the
"isconnected" variable correctly (SocketException is thrown: non-
blocking has this effect...) - but doesn''t appear to be a problem
because poll, read and write work fine.

For measuring the performance of the crawler, I started "perfmon.msc"
and added the "active connections" item from object "TCP". After a
while I found the number of this performance counter to reach over
300K connections (!), enough to start worrying...

My crawler is designed to support around 200 connections simultaneous.
"netstat -an" doesn''t support this finding, but does show hundreds of
connections that are in either "CLOSE_WAIT", "FIN_WAIT_2" or another
closing state.

After a host has completed, I try to disconnect the TCP/IP connection.
I''ve attempted combinations of "shutdown(both)", (async) "disconnect"
and "close(0)" - where no combination appears to have the desired
effect. When the application is shut down, all connections (including
the CLOSE_WAIT connections) are removed. The FIN_WAIT_2 connections
linger forever...

Perhaps someone knows a solution to this problem?

Greetings,

Stefan de Bruijn.

解决方案

On 25 Mar, 21:48, "atlaste" <atla...@gmail.comwrote:

Hi,

In an attempt to create a full-blown webcrawler I''ve found myself
writing a wrapper around the Socket class in an attempt to make it
completely async, supporting timeouts and some scheduling mechanisms.
I use a non-blocking approach for this, using the call to ''poll'' to
support the async mechanism - rather than the ''begin'' and ''end''
functions. I already found that connecting doesn''t set the
"isconnected" variable correctly (SocketException is thrown: non-
blocking has this effect...) - but doesn''t appear to be a problem
because poll, read and write work fine.

For measuring the performance of the crawler, I started "perfmon.msc"
and added the "active connections" item from object "TCP". After a
while I found the number of this performance counter to reach over
300K connections (!), enough to start worrying...

My crawler is designed to support around 200 connections simultaneous.
"netstat -an" doesn''t support this finding, but does show hundreds of
connections that are in either "CLOSE_WAIT", "FIN_WAIT_2" or another
closing state.

After a host has completed, I try to disconnect the TCP/IP connection.
I''ve attempted combinations of "shutdown(both)", (async) "disconnect"
and "close(0)" - where no combination appears to have the desired
effect. When the application is shut down, all connections (including
the CLOSE_WAIT connections) are removed. The FIN_WAIT_2 connections
linger forever...

Perhaps someone knows a solution to this problem?

Greetings,

Stefan de Bruijn.

Just a guess, can you try it with keepalive set to 0?

Something like where s is a Socket:

s.SetSocketOption(System.Net.Sockets.SocketOptionL evel.IP,
System.Net.Sockets.SocketOptionName.KeepAlive, 0);


Thanks for your reaction.

It is my understanding from TCP/IP keepalive have a different meaning
(see http://msdn.microsoft.com/library/de...et_options.asp
and http://msdn2.microsoft.com/en-us/lib...ptionname.aspx
) than what you would suggest. Setting it to "true" should close the
connection after 2 hours or something like that. (Since two hours
sounds like "a lifetime" to me, I can see where the name "keepalive"
originated :-)

So while it actually sound like a good idea to set it to true -
doesn''t really solve my problem I''m afraid.

Perhaps someone has another suggestion?

Cheers,
Stefan de Bruijn.
On Mar 26, 11:44 am, "DeveloperX" <nntp...@operamail.comwrote:

On 25 Mar, 21:48, "atlaste" <atla...@gmail.comwrote:

Hi,

In an attempt to create a full-blown webcrawler I''ve found myself
writing a wrapper around the Socket class in an attempt to make it
completely async, supporting timeouts and some scheduling mechanisms.
I use a non-blocking approach for this, using the call to ''poll'' to
support the async mechanism - rather than the ''begin'' and ''end''
functions. I already found that connecting doesn''t set the
"isconnected" variable correctly (SocketException is thrown: non-
blocking has this effect...) - but doesn''t appear to be a problem
because poll, read and write work fine.

For measuring the performance of the crawler, I started "perfmon.msc"
and added the "active connections" item from object "TCP". After a
while I found the number of this performance counter to reach over
300K connections (!), enough to start worrying...

My crawler is designed to support around 200 connections simultaneous.
"netstat -an" doesn''t support this finding, but does show hundreds of
connections that are in either "CLOSE_WAIT", "FIN_WAIT_2" or another
closing state.

After a host has completed, I try to disconnect the TCP/IP connection.
I''ve attempted combinations of "shutdown(both)", (async) "disconnect"
and "close(0)" - where no combination appears to have the desired
effect. When the application is shut down, all connections (including
the CLOSE_WAIT connections) are removed. The FIN_WAIT_2 connections
linger forever...

Perhaps someone knows a solution to this problem?

Greetings,

Stefan de Bruijn.


Just a guess, can you try it with keepalive set to 0?

Something like where s is a Socket:

s.SetSocketOption(System.Net.Sockets.SocketOptionL evel.IP,
System.Net.Sockets.SocketOptionName.KeepAlive, 0);



On 26 Mar, 12:43, "atlaste" <atla...@gmail.comwrote:

Thanks for your reaction.

It is my understanding from TCP/IP keepalive have a different meaning
(seehttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/wins...
andhttp://msdn2.microsoft.com/en-us/library/system.net.sockets.socketopt...
) than what you would suggest. Setting it to "true" should close the
connection after 2 hours or something like that. (Since two hours
sounds like "a lifetime" to me, I can see where the name "keepalive"
originated :-)

So while it actually sound like a good idea to set it to true -
doesn''t really solve my problem I''m afraid.

Perhaps someone has another suggestion?

Cheers,
Stefan de Bruijn.

On Mar 26, 11:44 am, "DeveloperX" <nntp...@operamail.comwrote:

On 25 Mar, 21:48, "atlaste" <atla...@gmail.comwrote:

Hi,

In an attempt to create a full-blown webcrawler I''ve found myself
writing a wrapper around the Socket class in an attempt to make it
completely async, supporting timeouts and some scheduling mechanisms.
I use a non-blocking approach for this, using the call to ''poll'' to
support the async mechanism - rather than the ''begin'' and ''end''
functions. I already found that connecting doesn''t set the
"isconnected" variable correctly (SocketException is thrown: non-
blocking has this effect...) - but doesn''t appear to be a problem
because poll, read and write work fine.

For measuring the performance of the crawler, I started "perfmon.msc"
and added the "active connections" item from object "TCP". After a
while I found the number of this performance counter to reach over
300K connections (!), enough to start worrying...

My crawler is designed to support around 200 connections simultaneous.
"netstat -an" doesn''t support this finding, but does show hundreds of
connections that are in either "CLOSE_WAIT", "FIN_WAIT_2" or another
closing state.

After a host has completed, I try to disconnect the TCP/IP connection.
I''ve attempted combinations of "shutdown(both)", (async) "disconnect"
and "close(0)" - where no combination appears to have the desired
effect. When the application is shut down, all connections (including
the CLOSE_WAIT connections) are removed. The FIN_WAIT_2 connections
linger forever...

Perhaps someone knows a solution to this problem?

Greetings,

Stefan de Bruijn.

Just a guess, can you try it with keepalive set to 0?

Something like where s is a Socket:

s.SetSocketOption(System.Net.Sockets.SocketOptionL evel.IP,
System.Net.Sockets.SocketOptionName.KeepAlive, 0);- Hide quoted text -


- Show quoted text -

Ah yes I see, how about changing the Linger option?

Basically the suggestion comes because of something I read about
Apache changing a method to close connections too become Linger_Close
and it caused lots of FIN_WAIT_2 issues for people.

Again, just an idea :)


这篇关于异步插座&amp;活动连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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