javascript - node js read specific message from tcp socket net.createServer - Stack Overflow

var net = require('net');var HOST = '0.0.0.0';var PORT = 5000; Create a server i

var net = require('net');

var HOST = '0.0.0.0';
var PORT = 5000;

// Create a server instance, and chain the listen function to it
// The function passed to net.createServer() bees the event handler for the 'connection' event
// The sock object the callback function receives UNIQUE for each connection
net.createServer(function(sock) {

// We have a connection - a socket object is assigned to the connection automatically
console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);

// Add a 'data' event handler to this instance of socket
sock.on('data', function(data) {

    console.log('DATA ' + sock.remoteAddress + ': ' + data);
    // Write the data back to the socket, the client will receive it as data from the server
    if (data === "exit") {
        console.log('exit message received !')
    }

});

// Add a 'close' event handler to this instance of socket
sock.on('close', function(data) {
    console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
});

}).listen(PORT, HOST);

console.log('Server listening on ' + HOST +':'+ PORT);

No matter what I try, I cannot get:

    if (data === "exit") {
        console.log('exit message received !')
    }

working, it's always false.

I'm connecting via telnet and sending "exit", the server should then go into the "if" loop and say "exit message received". This never happens, can someone shed some light ? thanks

var net = require('net');

var HOST = '0.0.0.0';
var PORT = 5000;

// Create a server instance, and chain the listen function to it
// The function passed to net.createServer() bees the event handler for the 'connection' event
// The sock object the callback function receives UNIQUE for each connection
net.createServer(function(sock) {

// We have a connection - a socket object is assigned to the connection automatically
console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);

// Add a 'data' event handler to this instance of socket
sock.on('data', function(data) {

    console.log('DATA ' + sock.remoteAddress + ': ' + data);
    // Write the data back to the socket, the client will receive it as data from the server
    if (data === "exit") {
        console.log('exit message received !')
    }

});

// Add a 'close' event handler to this instance of socket
sock.on('close', function(data) {
    console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
});

}).listen(PORT, HOST);

console.log('Server listening on ' + HOST +':'+ PORT);

No matter what I try, I cannot get:

    if (data === "exit") {
        console.log('exit message received !')
    }

working, it's always false.

I'm connecting via telnet and sending "exit", the server should then go into the "if" loop and say "exit message received". This never happens, can someone shed some light ? thanks

Share Improve this question asked Jan 27, 2016 at 11:44 biergardenerbiergardener 1851 gold badge1 silver badge11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

That's because data is not a string, if you try to pare with === you will get false because types don't match. To solve it you should pare the data object with a simple == or use socket.setEncoding('utf8') previous to binding the data event.

https://nodejs/api/net.html#net_event_data

var net = require('net');
var HOST = '0.0.0.0';
var PORT = 5000;

net.createServer(function(sock) {
    console.log('CONNECTED:',sock.remoteAddress,':',sock.remotePort);
    sock.setEncoding("utf8"); //set data encoding (either 'ascii', 'utf8', or 'base64')
    sock.on('data', function(data) {
        console.log('DATA',sock.remoteAddress,': ',data,typeof data,"===",typeof "exit");
        if(data === "exit") console.log('exit message received !');
    });

}).listen(PORT, HOST, function() {
    console.log("server accepting connections");
});

Note. If the data received is going to be big you should concatenate and handle the message parison at the end of it. Check other questions to handle those cases:

Node.js net library: getting plete data from 'data' event

I am aware this is quite an old post, when I tried to implement the code in the answer to this question I came across the same problem regardless of having used "==" or utf8 encoding. The issue for me turned out to be that the client I was using was appending a '\n' character to the end of the exit message thus causing the string parison to fail on the server. Perhaps this is not an issue with telnet and the like but this was the case with netcat. Hopefully this sheds some light for anyone else who es across this post and has the same problem that I did.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744854671a4597335.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信