问题:socket.shutdown与socket.close

我最近看到了一些看起来像这样的代码(袜子当然是套接字对象):

sock.shutdown(socket.SHUT_RDWR)
sock.close()

在套接字上调用shutdown然后关闭它的目的是什么?如果有所不同,则此套接字用于非阻塞IO。

I recently saw a bit of code that looked like this (with sock being a socket object of course):

sock.shutdown(socket.SHUT_RDWR)
sock.close()

What exactly is the purpose of calling shutdown on the socket and then closing it? If it makes a difference, this socket is being used for non-blocking IO.


回答 0

这是一个解释

一旦不再需要套接字,调用程序就可以通过对套接字描述符应用close子例程来丢弃该套接字。如果在关闭时可靠的传输套接字具有与之关联的数据,则系统将继续尝试进行数据传输。但是,如果仍未交付数据,则系统将丢弃该数据。如果应用程序不使用任何暂挂数据,则可以在关闭套接字之前使用套接字上的shutdown子例程。

Here’s one explanation:

Once a socket is no longer required, the calling program can discard the socket by applying a close subroutine to the socket descriptor. If a reliable delivery socket has data associated with it when a close takes place, the system continues to attempt data transfer. However, if the data is still undelivered, the system discards the data. Should the application program have no use for any pending data, it can use the shutdown subroutine on the socket prior to closing it.


回答 1

调用closeshutdown对基础套接字有两种不同的影响。

首先要指出的是,套接字是基础操作系统中的资源,并且多个进程可以具有同一基础套接字的句柄。

您打电话的时候 close它时,将句柄计数减一,如果句柄计数达到零,则套接字和关联的连接将通过正常的关闭过程(有效地将FIN / EOF发送到对等方)来释放套接字。

这里要注意的是,如果句柄计数没有达到零,因为另一个进程仍然具有套接字的句柄,则连接不会关闭并且套接字不会被释放。

另一方面,调用shutdown读写会关闭基础连接,并向对等方发送FIN / EOF,而不管套接字有多少个进程。但是,它不会取消分配套接字,您仍然需要在事后调用close。

Calling close and shutdown have two different effects on the underlying socket.

The first thing to point out is that the socket is a resource in the underlying OS and multiple processes can have a handle for the same underlying socket.

When you call close it decrements the handle count by one and if the handle count has reached zero then the socket and associated connection goes through the normal close procedure (effectively sending a FIN / EOF to the peer) and the socket is deallocated.

The thing to pay attention to here is that if the handle count does not reach zero because another process still has a handle to the socket then the connection is not closed and the socket is not deallocated.

On the other hand calling shutdown for reading and writing closes the underlying connection and sends a FIN / EOF to the peer regardless of how many processes have handles to the socket. However, it does not deallocate the socket and you still need to call close afterward.


回答 2

关闭和关闭的说明:正常关闭(msdn)

关机(针对您的情况)表示连接的另一端不再有读写套接字的意图。然后关闭释放与套接字关联的所有内存。

忽略关闭可能会导致套接字在操作系统堆栈中徘徊,直到正常关闭连接为止。

在国际海事组织中,“关闭”和“关闭”这两个名称具有误导性,“关闭”和“破坏”将强调它们之间的差异。

Explanation of shutdown and close: Graceful shutdown (msdn)

Shutdown (in your case) indicates to the other end of the connection there is no further intention to read from or write to the socket. Then close frees up any memory associated with the socket.

Omitting shutdown may cause the socket to linger in the OSs stack until the connection has been closed gracefully.

IMO the names ‘shutdown’ and ‘close’ are misleading, ‘close’ and ‘destroy’ would emphasise their differences.


回答 3

在Socket Programming HOWTO(py2 / py3)中已经提到了

断开连接

严格来说,应该先shutdown在套接字上使用close它。该shutdown是在另一端的咨询到插座。根据您传递的参数,它可能表示“ 我不再发送了,但我仍会听 ”,或“ 我不在听,很好的摆脱!”。但是,大多数套接字库都习惯于程序员忽略使用此礼节,通常a close与相同shutdown(); close()。因此,在大多数情况下,不需要显式关闭。

it’s mentioned right in the Socket Programming HOWTO (py2/py3)

Disconnecting

Strictly speaking, you’re supposed to use shutdown on a socket before you close it. The shutdown is an advisory to the socket at the other end. Depending on the argument you pass it, it can mean “I’m not going to send anymore, but I’ll still listen”, or “I’m not listening, good riddance!”. Most socket libraries, however, are so used to programmers neglecting to use this piece of etiquette that normally a close is the same as shutdown(); close(). So in most situations, an explicit shutdown is not needed.


回答 4

上面的代码难道不是错误的吗?

在shutdown调用之后直接执行close调用可能会使内核无论如何都丢弃所有传出缓冲区。

根据 http://blog.netherlabs.nl/articles/2009/01/18/the-ultimate-so_linger-page-or-why-is-my-tcp-not-reliable, 需要在关机和关机之间等待关闭,直到读取返回0。

Isn’t this code above wrong?

The close call directly after the shutdown call might make the kernel discard all outgoing buffers anyway.

According to http://blog.netherlabs.nl/articles/2009/01/18/the-ultimate-so_linger-page-or-why-is-my-tcp-not-reliable one needs to wait between the shutdown and the close until read returns 0.


回答 5

有一些关闭的方式:http : //msdn.microsoft.com/en-us/library/system.net.sockets.socket.shutdown.aspx。* nix是相似的。


回答 6

Shutdown(1),强制套接字no发送更多数据

这在

1-缓冲液冲洗

2-奇怪的错误检测

3-安全防护

让我解释更多,当您将数据从A发送到B时,不保证将其发送到B,仅保证将其发送到A os缓冲区,然后缓冲区又将其发送到B os缓冲区。

因此,通过在A上调用shutdown(1),您将刷新A的缓冲区,如果缓冲区不为空,则会引发错误,即:尚未将数据发送到对等方

但是,这是不可挽回的,因此您可以在完全发送完所有数据之后,并确保至少在对等os缓冲区中执行此操作

Shutdown(1) , forces the socket no to send any more data

This is usefull in

1- Buffer flushing

2- Strange error detection

3- Safe guarding

Let me explain more , when you send a data from A to B , it’s not guaranteed to be sent to B , it’s only guaranteed to be sent to the A os buffer , which in turn sends it to the B os buffer

So by calling shutdown(1) on A , you flush A’s buffer and an error is raised if the buffer is not empty ie: data has not been sent to the peer yet

Howoever this is irrevesable , so you can do that after you completely sent all your data and you want to be sure that it’s atleast at the peer os buffer


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。