gpt4 book ai didi

PHP:如何正确使用 socket_select() 和 socket_read()

转载 作者:搜寻专家 更新时间:2023-10-31 21:11:38 25 4
gpt4 key购买 nike

编辑: *测试工具中的错误导致我误解了结果。 socket_select() 的工作方式完全如您所料:它确实会等待,直到套接字上的数据准备就绪。但是如果你在客户端关闭连接后调用它,它会报告就绪。原来这都是我的错,但我会把问题留在这里以防其他人怀疑 socket_select() 的行为。

我有一个多线程 PHP 应用程序,我使用套接字在线程之间进行通信。通信工作得很好,但我最终做了很多没有准备好数据的套接字的不必要读取。也许我缺少有关套接字编程的某些东西。

PHP doc对于 socket_select() 表示 read 数组中列出的套接字将被监视以查看读取是否不会阻塞

这正是我想要的行为:调用 socket_select() 等待我的子线程之一尝试与我交谈。但这只适用于线程第一次写入,在我接受连接之后。之后,socket_select() 将永远说套接字已准备就绪,即使我已经从中读取了所有数据。

有没有什么方法可以将该套接字标记为“未就绪”,以便 socket_select() 在更多数据到达之前不会报告它已就绪?还是在我读取所有数据后关闭该连接并等待另一个连接请求是常规的?我已经阅读了很多教程和解释,但一直无法弄清楚如何正确地做到这一点。也许我遗漏了一些明显的东西?

如果它有助于查看代码,这就是我正在做的:

// Server side setup in the main thread
$this->mConnectionSocket = socket_create(AF_INET, SOCK_STREAM, 0);

$arrOpt = array('l_onoff' => 1, 'l_linger' => 0);
@socket_set_option($this->mConnectionSocket, SOL_SOCKET, SO_LINGER, $arrOpt);
@socket_set_option($this->mConnectionSocket, SOL_SOCKET, SO_REUSEADDR, true);
@socket_bind($this->mConnectionSocket, Hostname, $this->mPortNumber);
@socket_listen($this->mConnectionSocket);
@socket_set_block($this->mConnectionSocket);
.
.
.
// Then call listen(), which looks like this:
public function listen(&$outReadySockets) {
$null = null;

while(true) {
$readyArray = array_merge(array($this->mConnectionSocket), $this->mReceiverSockets);
socket_select($readyArray, $null, $null, $waitTime = null);

if(in_array($this->mConnectionSocket, $readyArray) === true) {
$this->acceptConnection();
$key = array_search($this->mConnectionSocket, $readyArray);

if($key === false) {
throw new IPCException("array_search() returned unexpected value");
} else {
unset($readyArray[$key]);
if(in_array($this->mConnectionSocket, $readyArray) === true) {
throw new IPCException("in_array() says the key is still there");
}
}
}

if(count($readyArray) > 0) {
$outReadySockets = array_merge($readyArray);
break;
}
}
}


// Client side setup in the child thread
$this->mSocket = @socket_create(AF_INET, SOCK_STREAM, 0);
@socket_set_block($this->mSocket);
@socket_connect($this->mSocket, Hostname, $this->mPortNumber);
.
.
.
@socket_write($this->mSocket, $inDataToWrite, $lengthToWrite);



// Main thread reads the socket until it's empty
$data = "";
$totalBytesRead = 0;
while($totalBytesRead < $inNumberOfBytesToRead) {
// Strange that even if we set the socket to block mode, socket_read()
// will not block. If there's nothing there, it will just return an
// empty string. This is documented in the PHP docs.
$tdata = socket_read($inSock, $inNumberOfBytesToRead);
if($tdata === false) {
throw new IPCException("socket_read() failed: " . socket_strerror(socket_last_error()));
} else {
$data .= $tdata;

$bytesReadThisPass = strlen($tdata);
if($bytesReadThisPass === 0) {
break;
}
}

$totalBytesRead += $bytesReadThisPass;
}
.
.
.
// Then calls listen() again

正如我所说,它工作得很好,除了当我第二次调用 listen() 时,它告诉我套接字仍然准备就绪。这似乎是 PHP 文档所说的,但我不想要那样。我想知道什么时候那里真的有数据。我做错了吗?或者只是错过了重点?

最佳答案

你滥用了套接字函数。

首先,socket_read 会在出错时返回false;您的代码不会对此进行检查,并将错误返回视为与空字符串相同(这是您正在使用的特定构造的产物,即字符串连接和 strlen)。

另一个问题是,当检测到连接尝试时,您显然违反了 socket_select 的说明:

No socket resource must be added to any set if you do not intend to check its result after the socket_select() call, and respond appropriately. After socket_select() returns, all socket resources in all arrays must be checked. Any socket resource that is available for writing must be written to, and any socket resource available for reading must be read from.

取而代之的是,如果检测到连接,代码会接受它并再次返回到 socket_select;准备好读取的套接字未得到服务。

最后,您似乎对 EOF 在套接字上的含义感到困惑:这意味着客户端已关闭连接的写入端。一旦 socket_read 第一次返回空字符串(不是 false!),它将永远不会再返回任何有意义的内容。一旦发生这种情况,您可以从服务器的写入端发送数据和/或完全关闭连接。

关于PHP:如何正确使用 socket_select() 和 socket_read(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18434695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com