2014-05-07 79 views
2

我试图在PHP和Node.JS创建的应用程序之间建立桥梁。通过UNIX套接字连接Node.JS和PHP - EPIPE写入错误

的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。为什么以及如何解决这个问题?

+1

在PHP你靠近管道的连接从它具有receieved数据后(c.write('你好\ r \ n')在节点js中)。当你的php进程不再监听时,节点js试图写入管道,那么它会失败。 –

+0

所以,我只需要使用一次'c.write',对吧? – Siper

+0

或者你可以让php继续读管道,直到它接收到一个特定的命令,然后关闭管道 –

回答

0

我知道问题在哪里。在PHP中,我在写入数据后关闭了管道的连接。节点尝试写入响应,但它不能,因为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); 
} 
0

试试这个;

<?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. 
} 
?> 
相关问题