gpt4 book ai didi

php - 通过 UNIX 套接字连接 Node.JS 和 PHP - EPIPE 写入错误

转载 作者:太空宇宙 更新时间:2023-11-03 22:37:37 24 4
gpt4 key购买 nike

我正在尝试在 PHP 和 Node.JS 创建的应用程序之间建立一座桥梁。

Node.JS 创建套接字并监听它,我的代码:

var net = require('net'),
fs = require('fs');
var path = '/tmp/echo.sock';

fs.unlink(path, function () {
var server = net.createServer(function(c) {

console.log('server connected');

c.on('close', function() {
console.log('server disconnected');
});
c.write('hello\r\n');

c.on('data', function(data) {
console.log('Response: "' + data + '"');
c.write('You said "' + data + '"');
});

});
server.listen(path, function(e) {
console.log('server bound on %s', path);
});
});

process.on('uncaughtException', function (err) {
console.log( "UNCAUGHT EXCEPTION " );
console.log( "[Inside 'uncaughtException' event] " + err.stack || err.message );
});

我的 PHP 代码只是与现有套接字连接并发送一些数据:

$fp = fsockopen("unix:///tmp/echo.sock", -1, $errno, $errstr);
if (!$fp) {
return "ERROR: $errno - $errstr<br />\n";
} else {
fwrite($fp, "Hello World <3");
$out = fread($fp, 8192);
fclose($fp);
return $out; // That code is in function.
}

一切都应该正常,但在 Node.JS 控制台中我看到响应:

server bound on /tmp/echo.sock
server connected
Response: "Hello World <3"
UNCAUGHT EXCEPTION
[Inside 'uncaughtException' event] Error: write EPIPE
at exports._errnoException (util.js:745:11)
at Object.afterWrite (net.js:763:14)
server disconnected

在 PHP 中我只看到第一条消息,hello。为什么以及如何解决这个问题?

最佳答案

试试这个;

<?php
$fp = fsockopen("unix:///tmp/echo.sock", -1, $errno, $errstr);
if (!$fp) {
return "ERROR: $errno - $errstr<br />\n";
} else {
$out = fread($fp, 8192);
fwrite($fp, "Hello World <3");
$out2 = fread($fp, 8192);
fclose($fp);
echo $out; // That code is in function.
echo $out2; // That code is in function.
}
?>

关于php - 通过 UNIX 套接字连接 Node.JS 和 PHP - EPIPE 写入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23513791/

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