Guest User

Untitled

a guest
Sep 18th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. String comparison fails
  2. var authenticateClient = function(client) {
  3. client.write("Enter your username:");
  4. var username = "eleeist";
  5. client.on("data", function(data) {
  6. if (data == username) {
  7. client.write("username success");
  8. } else {
  9. client.write("username failure");
  10. }
  11. });
  12. }
  13.  
  14. var net = require("net");
  15. var server = net.createServer(function(client) {
  16. console.log("Server has started.");
  17. client.on("connect", function() {
  18. console.log("Client has connected.");
  19. client.write("Hello!");
  20. authenticateClient(client);
  21. });
  22. client.on("end", function() {
  23. console.log("Client has disconnected.");
  24. });
  25. }).listen(8124);
  26.  
  27. var HOST = 'localhost';
  28. var PORT = '8124';
  29.  
  30. var authenticateClient = function(client) {
  31. client.write("Enter your username:");
  32. var username = "eleeist";
  33. client.on("data", function(data) {
  34. console.log('data as buffer: ',data);
  35. data= data.toString('utf-8').trim();
  36. console.log('data as string: ', data);
  37. if (data == username) {
  38. client.write("username success");
  39. } else {
  40. client.write("username failure");
  41. }
  42. });
  43. }
  44.  
  45. var net = require("net");
  46. var server = net.createServer(function(client) {
  47. console.log("Server has started.");
  48. client.on("connect", function() {
  49. console.log("Client has connected.");
  50. client.write("Hello!");
  51. authenticateClient(client);
  52. });
  53. client.on("end", function() {
  54. console.log("Client has disconnected.");
  55. });
  56. }).listen(PORT);
  57.  
  58. //CLIENT
  59. console.log('creating client');
  60. var client = new net.Socket();
  61. client.connect (PORT, HOST, function() {
  62. console.log('CONNECTED TO: ' + HOST + ':' + PORT);
  63. client.write('eleeistn');
  64. });
  65. client.on('data', function(data) {
  66. console.log('DATA: ' + data);
  67. // Close the client socket completely
  68. // client.destroy();
  69. });
  70.  
  71. client.on('error', function(exception){ console.log('Exception:' , exception); });
  72. client.on('timeout', function() { console.log("timeout!"); });
  73. client.on('close', function() { console.log('Connection closed'); });
Add Comment
Please, Sign In to add comment