我需要将PHP代码迁移到Nodejs。该PHP代码使用套接字建立与另一台服务器的连接,写入一个文件,然后获取另一个文件的结果。
代码如下:(这是我的代码的摘要示例)
$compilerhost = "xxx.xxx.x.x"
$compilerport = 1111;
$socket = fsockopen($compilerhost, $compilerport); //connect to server via socket
if($socket)
{
fwrite($socket, "data1"."\n"); // write file line 1
fwrite($socket, "data2"."\n"); // write file line 2
$result = fgets($socket); // get results
echo $result;
}
我在Nodejs中的代码:
var fs = require('fs');
var net = require('net');
var socket = new net.Socket();
socket.connect (port, host, function() {
console.log('CONNECTED TO: ' + compilerHost + ':' + compilerPort);
fs.writeFile(socket, "test", function(err) {
if(err) {
console.log(err);
}
});
});