Hi Community :)
I've been working on a NodeJS Server that receives a string, compares the received string with hard-coded strings, and if it finds a match, it reacts on the string.
sock.on('data', function(data) {
console.log('DATA ' + sock.remoteAddress + ': ' + String(data));
var ReferencedData = '';
ReferencedData += data;
if (ReferencedData == ShutDown)
{
console.log('Incoming Shutdown Command... will be executed');
ClearTemp();
console.log('Exiting');
process.exit(code=0);
}
If i try it out using "Helloworld" as string, the Terminal says "Data XX.XXX.XXX.XX:XXXX Helloworld", this means the String has been fully received and can be read.
The problem occurs when i start comparing,
i tried this:
(data == 'ShutDown')
(String(data) == 'ShutDown')
var Shutdown = 'ShutDown'
and then
(data == Shutdown)
and
(data == String(Shutdown))
But, from the output, it seems that data and my received string don't match-
The Software does not react on this.
i tried every possibility that came to my mind but it does not work,
none of my if-statements work on the String, seems like the received string contains some additional DATA, this seems to make my comparison impossible.
Can somebody help me by telling me how to do this the right way?
Thank you all in advance :)
Edit:
I do not use any Encryption, this is the important part of my code
var ShutDown = 'Shutdown';
var HowMany = 'Howmany';
function RunServer()
{
net.createServer(function(sock) {
console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);
/
sock.on('data', function(data) {
console.log('DATA ' + sock.remoteAddress + ': ' + String(data));
var RemoteAdressAndPort = sock.remoteAddress + ':' + sock.remotePort;
if (data == ShutDown)
{
console.log('Incoming Shutdown Command... will be executed');
ClearTemp();
console.log('Exiting');
process.exit(code=0);
}
if (data == HowMany)
{
console.log('Client asks me how many...');
sock.write(NumberOfItems);
sock.end();
}
});
sock.on('close', function(data) {
console.log('CLOSED: ' + RemoteAdressAndPort);
});
}).listen(PORT, HOST);
console.log('Server listening on ' + HOST +':'+ PORT);
};