Question

I'm trying to create a bridge between my application created in PHP and Node.JS.

Node.JS creates socket and listening to it, my code:

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

And my PHP code just connecting with exists socket and send some data:

$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.
}

Everything should working, but in Node.JS console I see response:

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

And in PHP I see just first message, hello. Why and how can I fix that?

Was it helpful?

Solution 2

I know where's the problem. In PHP I close the connection to the pipe after write data. Node tries to write response but it can't, because connection in PHP is already closed. So, I just added checking is the connection still up. Of course, this below is not used in production, but just to show how it works.

Node.JS Code:

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

And in 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);
}

OTHER TIPS

try this;

<?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.
}
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top