Write to Node.js socket

时间:2016-08-30 04:33:15

标签: node.js

I'm trying to send a string from Node.js to a Java server but nothing will send from the Node.js client unless I call client.end() after. I'm not very experienced with Node.js so any suggestions would help.

var net = require('net');

var client = net.connect(1032, 'localhost')

client.on('connect', function(){
    console.log('connected');
});

client.on('data', function(data){
    console.log(data.toString());
    client.write('test reply');
});

client.on('close', function(){
    client.end();
});

1 个答案:

答案 0 :(得分:1)

var net = require('net');

var client = net.connect(1032, 'localhost')

client.on('connect', function(){
    console.log('connected');
    client.write('test reply');
});

client.on('data', function(data){
    console.log(data.toString());

});

client.on('close', function(){
    client.end();
});