如果您想重複使用地址和端口,並擺脫錯誤:無法綁定,地址已使用中,您必須在呼叫 bind 之前使用 socket_setopt(請在您的 PHP 版本中檢查此函式的實際拼寫)
<?php
if (!socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo socket_strerror(socket_last_error($sock));
exit;
}
?>
此解決方案是由
Christophe Dirac 發現的。感謝 Christophe!