停止在套接字上听? [英] Stop listening on a socket?

查看:72
本文介绍了停止在套接字上听?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用套接字。我接受使用

m_mySocket.Listen(BackLogCount)的连接;但是当我想停止收听时,我会关闭所有客户端并调用m_mySocket.Close(),但它总是引发一个

OnConnect事件(实际上,它调用了回调函数好像有一个

的新连接尝试)并且我收到一个ObjectDisposedException,我就接受了
做m_mySocket.EndAccept。


有没有人知道我能做些什么呢?我不希望

依赖异常作为正常过程...


谢谢


ThunderMusic

解决方案

ThunderMusic写道:




我我目前正在使用套接字。我接受使用

m_mySocket.Listen(BackLogCount)的连接;但是当我想停止收听时,我会关闭所有客户端并调用m_mySocket.Close(),但它总是引发一个

OnConnect事件(实际上,它调用了回调函数好像有一个

的新连接尝试)并且我收到一个ObjectDisposedException,我就接受了
做m_mySocket.EndAccept。


有没有人知道我能做些什么呢?我不希望

依赖异常作为正常过程...



我想这取决于你对正常的定义过程" ;. :)


在某种意义上,套接字的关闭_is_例外情况。

也就是说,通常套接字只是坐在那里等待连接

请求。特殊情况是你关闭套接字。


在任何情况下,AFAIK接收ObjectDisposedException _is_正常

方式来检测这种情况下的套接字关闭。优秀的i / o

BeginAccept我需要完成的i / o请求,一旦你打电话给

Socket.Close(),那么EndAccept就没有别的了可以做的除了

抛出专门设计的异常,表明已经调用了

Socket.Close()。


例外情况不仅仅适用于某些事情被破坏的情况。它们只是表示特殊代码路径的方式。确实,错误导致很多特殊情况,但我不认为人们应该担心

关于确保_only_错误导致异常案件。

有时一个例外情况是预期的结果,但通常结果并不是一般的结果。


Pete




" Peter Duniho" < Np ********* @NnOwSlPiAnMk.com在留言中写道

新闻:13 ************* @ corp.supernews.com .. 。


ThunderMusic写道:


>
我正在工作带插座。我使用
m_mySocket.Listen(BackLogCount)接受连接;但是当我想停止收听时,我会关闭所有客户端并调用m_mySocket.Close(),但它总是会引发一个OnConnect事件(实际上,它会调用回调函数,就好像有是一个新的连接尝试)我收到一个ObjectDisposedException就像我做m_mySocket.EndAccept一样。

有谁知道我能做些什么呢?我宁愿
不要依赖异常作为正常的过程...



我想这取决于你对正常过程的定义。 :)


从某种意义上说,套接字的关闭_is_是例外情况。那个

,通常是套接字就在那里等待连接请求。

特殊情况是关闭套接字时。


在任何情况下,AFAIK接收ObjectDisposedException _is_正常

方式来检测这种情况下的套接字关闭。优秀的i / o

BeginAccept我需要完成的i / o请求,一旦你打电话给

Socket.Close(),那么EndAccept就没有别的了可以做但除了抛出

特殊设计的异常,表明已经调用了

Socket.Close()。


例外情况不仅仅适用于某些事情被破坏的情况。它们只是表示特殊代码路径的方式

。确实,错误导致了很多例外情况,但我不认为人们应该担心

确保_only_错误导致例外情况。有时

特殊情况是预期结果,但根本不是通常的结果。


Pete



我的意思是正常流程我将永远做我能做的一切

避免异常,但如果我没有任何选择,我会抓住

例外......我意思是......有一个清洁的关闭套接字的方法?它是完全正常的,并且不是一件特别的事情,重启它的计算机以获得关键更新或任何其他原因...应用程序将关闭一次

一段时间......最重要的是,我将在我的应用程序中使用一个功能来接受连接并停止接受连接......我可以通过用含有状态变量的
,但是它仍然会监听端口...


所以,如果我理解得很好,那么停止听的唯一方法就是是在套接字上调用停止

它会调用EndAccept回调...对吗?


谢谢

ThunderMusic


ThunderMusic写道:


我的意思是正常流程我将永远做我能做的一切

避免异常,但如果我没有任何选择,我会抓住

例外...



我的观点是异常不一定是需要避免
的东西。


我的意思是......是否有更干净的关闭套接字的方法?



No.


这是

完全正常而不是重启它计算机的特殊事情

关键更新或任何其他原因...



我不同意。虽然这不是一个错误或者需要一个问题,但是需要安装一个关键更新(例如),它肯定是一个

例外。情况。也就是说,这不是通常的事态。

大多数时候你都可以通过而不必这样做。


_normal_情况不是必须安装关键更新。

每个月大约一个月,你有一个特殊的日子,需要安装一个关键的

更新。
< blockquote class =post_quotes>
应用程序将偶尔关闭...



但这是我的观点。仅仅因为它可能需要发生b $ b b,这并不意味着它不是一个例外事件。


,最重要的是,我将在我的应用程序中有一个功能,以便

接受连接并停止接受连接...我可以手动完成

带状态变量,但它仍然会监听端口...



正确。


所以,如果我理解,停止收听的唯一方法是在套接字上调用停止

,它会调用EndAccept回调...对吗?



如果通过停止你的意思是关闭,然后是。


Pete


Hi,
I''m currently working with sockets. I accept connections using
m_mySocket.Listen(BackLogCount); But when I want to stop listening, I
shutdown all my clients and call m_mySocket.Close(), but it always raise a
OnConnect event (actually, it calls the callback function as if there was a
new connection attempt) and I receive a ObjectDisposedException as soon as I
do m_mySocket.EndAccept.

Does anyone have any idea of what I could do about it? I would prefer not
to rely on exceptions as the normal process...

Thanks

ThunderMusic

解决方案

ThunderMusic wrote:

Hi,
I''m currently working with sockets. I accept connections using
m_mySocket.Listen(BackLogCount); But when I want to stop listening, I
shutdown all my clients and call m_mySocket.Close(), but it always raise a
OnConnect event (actually, it calls the callback function as if there was a
new connection attempt) and I receive a ObjectDisposedException as soon as I
do m_mySocket.EndAccept.

Does anyone have any idea of what I could do about it? I would prefer not
to rely on exceptions as the normal process...

I guess that depends on your definition of "normal process". :)

In some sense, the closure of the socket _is_ the exceptional case.
That is, normally the socket just sits there waiting for a connection
request. The exceptional case is when you close the socket.

In any case, AFAIK receiving the ObjectDisposedException _is_ the normal
way to detect socket closure in this situation. The outstanding i/o
BeginAccept i/o request you made needs to complete, and once you call
Socket.Close(), there''s nothing else that EndAccept could do except
throw the exception that is specifically designed to indicate that
Socket.Close() has been called.

Exceptions aren''t just for when something is broken. They are simply
ways to indicate the exceptional code path. It''s true that errors cause
a lot of the exceptional cases, but I don''t think that one should worry
about making sure that _only_ errors cause the exceptional cases.
Sometimes an exceptional case is an expected outcome, but is simply not
the usual outcome.

Pete



"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...

ThunderMusic wrote:

>Hi,
I''m currently working with sockets. I accept connections using
m_mySocket.Listen(BackLogCount); But when I want to stop listening, I
shutdown all my clients and call m_mySocket.Close(), but it always raise
a OnConnect event (actually, it calls the callback function as if there
was a new connection attempt) and I receive a ObjectDisposedException as
soon as I do m_mySocket.EndAccept.

Does anyone have any idea of what I could do about it? I would prefer
not to rely on exceptions as the normal process...


I guess that depends on your definition of "normal process". :)

In some sense, the closure of the socket _is_ the exceptional case. That
is, normally the socket just sits there waiting for a connection request.
The exceptional case is when you close the socket.

In any case, AFAIK receiving the ObjectDisposedException _is_ the normal
way to detect socket closure in this situation. The outstanding i/o
BeginAccept i/o request you made needs to complete, and once you call
Socket.Close(), there''s nothing else that EndAccept could do except throw
the exception that is specifically designed to indicate that
Socket.Close() has been called.

Exceptions aren''t just for when something is broken. They are simply ways
to indicate the exceptional code path. It''s true that errors cause a lot
of the exceptional cases, but I don''t think that one should worry about
making sure that _only_ errors cause the exceptional cases. Sometimes an
exceptional case is an expected outcome, but is simply not the usual
outcome.

Pete

What I mean by "normal process" is I will always do everything I can do to
avoid an exception, but if I don''t have any choice, I will catch the
exception... I mean... Is there a "cleaner" way of closing a socket? It''s
perfectly normal and not an exceptional thing to reboot it''s computer for
critical updates or any other reason... the application will be closed once
in a while... and most of all, I will have a functionnality in my app to
accept connections and stop accepting connections... I could do it by hand
with a state variable, but it would still be listening the port...

So, if I understand well, the only way to stop listening is to call "Stop"
on the socket and it will call the EndAccept callback... right?

Thanks

ThunderMusic


ThunderMusic wrote:

What I mean by "normal process" is I will always do everything I can do to
avoid an exception, but if I don''t have any choice, I will catch the
exception...

My point is that exceptions are not necessarily something that need to
be avoided.

I mean... Is there a "cleaner" way of closing a socket?

No.

It''s
perfectly normal and not an exceptional thing to reboot it''s computer for
critical updates or any other reason...

I disagree. While it''s not an error or otherwise a problem to need to
install a critical update (for example), it most certainly is an
"exceptional" situation. That is, it is not the usual state of affairs.
Most days you can get through without having to do that.

The _normal_ situation is to not have to install a critical update.
Once a month or so, you have an exceptional day, on which a critical
update needs to be installed.

the application will be closed once in a while...

But that''s my point. Just because it''s something that might have to
happen, that doesn''t mean it''s not an exceptional event.

and most of all, I will have a functionnality in my app to
accept connections and stop accepting connections... I could do it by hand
with a state variable, but it would still be listening the port...

Correct.

So, if I understand well, the only way to stop listening is to call "Stop"
on the socket and it will call the EndAccept callback... right?

If by "Stop" you actually mean "Close", then yes.

Pete


这篇关于停止在套接字上听?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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