C#中Socket关闭 Close、Dispose、Shutdown、Disconnect
2017-03-10 15:37
821 查看
An answer on StackOverflow made
me think I have finally reached some glimpse of an understanding. Then I went testing for a bit and here's the summary of a newbie's view. Please correct me if I'm wrong because this is based on inference, not expertise.
the
depending on the provided argument. It doesn't disable the underlying protocol handling and it never blocks.
If
disabled, it also queues up a zero-byte send packet into the underlying send buffer. When the other side receives this packet, it knows that your socket will no longer send any data.
If
disabled, any data the other side might be trying to send will be lost.
If
disabled without disabling
it just prevents the socket from receiving data. Since no zero-byte packet will be sent, the other side won't know anything about it until it tries to send something, and only if the socket's protocol requires acknowledging.
First,
the equivalent of
Then it blocks, waiting for two things:
For all the queued-up send data to be sent.
For the other side to acknowledge the zero-byte packet (if applicable to the underlying protocol).
If you call
system resources will be freed.
system resources. May abruptly stop sending queued-up data. If called with the argument, will wait for the data to be sent, but only up to the specified timeout.
same as the
without the timeout argument. To be more precise,
timeout is the same as
If you use the
on the socket, it will automatically call
me think I have finally reached some glimpse of an understanding. Then I went testing for a bit and here's the summary of a newbie's view. Please correct me if I'm wrong because this is based on inference, not expertise.
Shutdown
Shutdowndisables
the
Sendand/or
Receivemethods,
depending on the provided argument. It doesn't disable the underlying protocol handling and it never blocks.
If
Sendis
disabled, it also queues up a zero-byte send packet into the underlying send buffer. When the other side receives this packet, it knows that your socket will no longer send any data.
If
Receiveis
disabled, any data the other side might be trying to send will be lost.
If
Receiveis
disabled without disabling
Send,
it just prevents the socket from receiving data. Since no zero-byte packet will be sent, the other side won't know anything about it until it tries to send something, and only if the socket's protocol requires acknowledging.
Disconnect
First, Disconnectdoes
the equivalent of
Shutdown(SocketShutdown.Both).
Then it blocks, waiting for two things:
For all the queued-up send data to be sent.
For the other side to acknowledge the zero-byte packet (if applicable to the underlying protocol).
If you call
Disconnect(false),
system resources will be freed.
Close
Closefrees
system resources. May abruptly stop sending queued-up data. If called with the argument, will wait for the data to be sent, but only up to the specified timeout.
Dispose
Disposeis
same as the
Closeoverload
without the timeout argument. To be more precise,
Closewithout
timeout is the same as
Dispose.
If you use the
usingblock
on the socket, it will automatically call
Dispose.
相关文章推荐
- C#中Socket关闭 Close、Dispose、Shutdown、Disconnect
- Linux socket关闭连接shutdown与close
- Socket关闭, shutdown, close, linger
- socket关闭的close和shutdown区别
- socket关闭之close()和shutdown()的差异
- 转:Linux socket关闭连接shutdown与close
- socket关闭的close和shutdown区别
- socket关闭操作close和shutdown
- socket编程——关闭(shutdown 和 close的区别)
- socket半关闭 shutdown close 区别
- 用于关闭socket 的close 和shutdown 区别
- socket链接的关闭close和shutdown的区别_TIME_WAIT和CLOSE_WAIT什么时刻出现_如何处理
- socket关闭的close和shutdown区别
- socket关闭close和shutdown区别
- socket关闭close和shutdown(转)
- socket链接的关闭close和shutdown的区别,TIME_WAIT和CLOSE_WAIT什么时刻出现,如何处理
- socket关闭close和shutdown
- socket关闭的close和shutdown区别
- socket关闭的close和shutdown区别
- socket关闭的close和shutdown区别