Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

JavaScript
  sock.on('data', function(data) {
        
       console.log('DATA ' + sock.remoteAddress + ': ' + String(data)); //receive
	   var ReferencedData = '';
ReferencedData += data;	   
       if (ReferencedData == ShutDown) //UNSECURE, should need permissions
       {
	   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:
JavaScript
(data == 'ShutDown')
(String(data) == 'ShutDown')

JavaScript
var Shutdown = 'ShutDown'
and then
JavaScript
(data == Shutdown)

and
JavaScript
(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

JavaScript
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)); //receive
	   var RemoteAdressAndPort = sock.remoteAddress + ':' + sock.remotePort;
	
       if (data == ShutDown) //UNSECURE
       {
	   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.pipe(sock); //Tried this out, don't actually now what it does
    });
    
	
	//Socket Close Function
    sock.on('close', function(data) {
        console.log('CLOSED: ' + RemoteAdressAndPort);
    });
    
}).listen(PORT, HOST);

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

};
Posted
Updated 20-Aug-14 3:07am
v4
Comments
[no name] 20-Aug-14 7:18am    
If you are comparing like you say you are using the = then that is not comparing, that is assigning. You use == to compare.

1 solution

if your tcp is encrypt each time you check your data string it will have different result.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900