©
Ce document utilise Manuel du site Web PHP chinois Libérer
(PHP 4 >= 4.1.0, PHP 5)
socket_close — 关闭套接字资源
$socket
) socket_close() 会关闭掉给定的 socket
资源。
这个函数只针对套接字资源有效,不能用在其他类型的资源类型上。
socket
由 socket_create() 或者是 socket_accept() 创建的有效的套接字资源。
没有返回值。
[#1] aeolianmeson at NOSPAM dot blitzeclipse dot com [2006-05-29 22:24:27]
PHP: 5.1.4
Summary: close() does not relinquish socket immediately.
With the BSD socket implementation (which is the socket interface used by PHP), a socket_close() may close the socket, but there may yet be data to send. Until the data is sent, the port will not be available. Therefore, all further bindings attempted on that port will not be acceptable due to the 'port can not be reused' (the approximate message, anyway) error. Ordinarily, if the REUSABLE socket option is set, the only thing that will raise such an error is a binding to a specific IP/PORT combination that is already bound.
To avoid this problem, you must tell it to delay returning until the port either sends the rest of its data, or times-out in doing it. This is done via the SO_LINGER option. To set this option, it requires an array of two elements: the first indicates whether a linger is required on any data before the shutdown completes, and the second indicates whether we actually linger. If we set a nonzero to the first while setting a zero to the second, we would simply drop whatever data is waiting in the buffer, and close the socket. To tell it simply to wait on the data to be sent, you send a non-zero for both: array(1, 1).
Note that if you have indicated not to block (socket_set_nonblock()), it will simply exit no matter what, much like it usually would. In this case it bursts an EWOULDWAIT flag, but since I don't think we have access to these socket flags in PHP, one should re-enable blocking right before they set the linger and quit.
// These commands get fed straight through to the Unix socket libraries.. That's why they're a little more C-like.
$arrOpt = array('l_onoff' => 1, 'l_linger' => 1);
socket_set_block($this->Socket);
socket_set_option($this->Socket, SOL_SOCKET, SO_LINGER, $arrOpt);
socket_close($this->Socket);
[#2] chaos [2002-06-07 15:57:46]
Sometimes it seems that you have to shutdown() a socket before you can close() it. I experienced that while making a chatserver in PHP with this cool socket extension.