微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在PHP中:OpenSSL错误消息:错误:1409F07F:SSL例程:SSL3_WRITE_PENDING:写入错误重试

我正在尝试使用PHP中的SSL / TLS连接发送大量数据.如果数据块不是很大或者我不使用TLS,但是我需要(接近2MiB),fwrite函数显示警告:

Warning: fwrite(): SSL operation Failed with code 1. OpenSSL Error messages: error: 1409F07F: SSL routines: SSL3_WRITE_PENDING: bad write retry

我用来连接客户端的相关代码

$cntxt = stream_context_create(array('ssl' => array('local_cert' => 'certificate.pem')));
$server = stream_socket_server('tls://127.0.0.1:8080', $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $cntxt);

// Wait for client connection //

$client = stream_socket_accept($server);
// Use non-blocking socket to allow answering many clients at a time
stream_set_blocking($client, 0);
$clients[] = $client;

发送数据时,它会附加到缓冲区,并且有时会为每个客户端和链接缓冲区调用函数

function trySend($client, &$buffer) {
    if (strlen($buffer)) {
        $len = fwrite($client, $buffer);
        $buffer = substr($buffer, $len);
    }
}

正如我所说,我的代码适用于少量数据或普通(非TLS)连接.我搜索了这个错误并找到了http://www.openssl.org/docs/ssl/SSL_write.html

SSL_write() will only return with success, when the complete contents of buf of length num has been written. This default behavIoUr can be changed with the SSL_MODE_ENABLE_PARTIAL_WRITE option of SSL_CTX_set_mode(3). When this flag is set, SSL_write() will also return with success, when a partial write has been successfully completed. In this case the SSL_write() operation is considered completed. The bytes are sent and a new SSL_write() operation with a new buffer (with the already sent bytes removed) must be started. A partial write is performed with the size of a message block, which is 16kB for SSLv3/TLSv1.

但是我怎么能用PHP做到这一点?

任何帮助赞赏:)

解决方法:

我发现我可以通过将传递给fwrite()的字符串的长度限制为8192来解决这个问题,这会阻止fwrite()警告.

所以对于你的例子中的代码,我会尝试将substr()调用更改为:

$buffer = substr($buffer, $len, 8192);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐