douduiti3040 2018-08-29 15:20
浏览 112

将php代码迁移到nodejs(套接字)

I need to migrate PHP code to Nodejs. This PHP code makes a connection to another server using socket, writes a file and then takes the result of another file.

The code is as follows: (it is a summary example of my code)

$compilerhost = "xxx.xxx.x.x"
$compilerport = 1111;

$socket = fsockopen($compilerhost, $compilerport); //connect to server via socket

if($socket)
{
    fwrite($socket, "data1"."
"); // write file line 1

    fwrite($socket, "data2"."
"); // write file line 2

    $result = fgets($socket); // get results

    echo $result;
}

My code in 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);
        }
    });                     
});
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 个人网站被恶意大量访问,怎么办
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)