通过UNIX套接字在Node.JS和PHP之间进行连接-EPIPE写入错误

以上

我正在尝试在用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中,我在写入数据后关闭了与管道的连接。Node尝试编写响应,但不能写响应,因为PHP中的连接已关闭。因此,我刚刚添加了检查连接是否仍然正常。当然,下面的代码不会在生产中使用,而只是为了说明其工作原理。

Node.JS代码:

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

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 + '"');

        if( data == "IS_ALREADY_CLOSED" & toSend == sended ) {
           c.write('ALREADY_CLOSED');     
           return;
        } else {
           c.write('NOT_ALREADY_CLOSED');
           return;
        }  
        c.write('You said "' + data + '"'); 
        sended++;
    });

  });
  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");

   do {
       $output = fread($fp, 8192);
       if($output == "ALREADY_CLOSED") break;
       elseif( $output == "NOT_ALREADY_CLOSED" ) continue;

       echo $output . "\n";
       fwrite($fp, "IS_ALREADY_CLOSED");
   } while(true);

   fclose($fp);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

通过UNIX套接字在Node.JS和PHP之间进行连接-EPIPE写入错误

来自分类Dev

Node.js-我不断收到以下错误:错误:ffmpeg流:写入EPIPE

来自分类Dev

Node.js-错误:编写EPIPE代码:'EPIPE

来自分类Dev

Node.js套接字连接错误

来自分类Dev

错误:使用node.js和imagemagick编写EPIPE

来自分类Dev

ava测试运行程序写入EPIPE和ECONNRESET错误

来自分类Dev

写入uWSGI Unix套接字

来自分类Dev

写入uWSGI Unix套接字

来自分类Dev

Mosquitto MQTT连接套接字错误(通过PLC连接)

来自分类Dev

对等方重置连接:FTP期间套接字写入错误

来自分类Dev

通过套接字发送和接收照片错误

来自分类Dev

PHP和C ++之间的套接字连接

来自分类Dev

node.js-处理TCP套接字错误ECONNREFUSED

来自分类Dev

Node.js MongoDB套接字关闭错误

来自分类Dev

Node.js https.request()错误:套接字挂断

来自分类Dev

使用https的Node.js中的套接字挂起错误

来自分类Dev

Node.JS,套接字IO:相同的源策略错误

来自分类Dev

UNIX套接字连接被拒绝

来自分类Dev

PHP侦听unix套接字

来自分类Dev

使用node.js连接到已经建立的UNIX套接字吗?

来自分类Dev

Node.js-events.js:154抛出错误,写EPIPE; 程序崩溃

来自分类Dev

错误:在连接mongodb的Windows上没有UNIX套接字支持

来自分类Dev

Postgres DB无法在Mac OSX上启动:错误提示:Unix域套接字上的连接

来自分类Dev

通过NetworkStream(套接字)写入/读取字符串以进行聊天

来自分类Dev

通过Unix套接字将DBeaver连接到远程PostgreSQL DB

来自分类Dev

如何通过Unix套接字将PhpMyAdmin连接到本地主机?

来自分类Dev

套接字编程连接错误

来自分类Dev

如何通过Rust中的UNIX套接字发送和收听数据?

来自分类Dev

Unix套接字文件名错误

Related 相关文章

热门标签

归档