5

New at stackoverflow and new with Node.

I have one simple socket server and one web server.

I want the web server to send a message to the socket server if someone connected the web server.

Browser <=> Web Server/Socket Client <=> Socket Server

I created the server like this :

var http = require('http');
var net = require('net');
var HOST = '192.168.1.254';
var PORT = 8888;
var client = new net.Socket();

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n', function(){
      client.connect(PORT, HOST, function() {
        console.log('Connected To: ' + HOST + ':' + PORT);
        client.write('0109001' + "\n");
      });   

    // Add a 'data' event handler for the client socket
    // data is what the server sent to this socket
        client.on('data', function(data) {
            console.log('The Data: ' + data);
            client.destroy();
        });

        // Add a 'close' event handler for the client socket
        client.on('close', function() {
            console.log('Connection closed');
        });
  });
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

This code works but the web server sending the message to the socket server twice. Did I do something wrong maybe with the callback or something? Any help would be appreciated.

BigBoss
  • 113
  • 1
  • 1
  • 5
  • 1
    are you sure you are sending only one request? Dont forget the favicon request every browser does – pfried Mar 06 '13 at 07:56
  • 1
    Try adding [`console.log(req.url);`](http://nodejs.org/api/http.html#http_request_url). Does it output the same URL each time? – Jonathan Lonowski Mar 06 '13 at 08:07

2 Answers2

12

If you call this code by a browser it sends 2 requests to the server. One for the FavIcon and one for the page itself.

The code inside the callback is called twice because there are 2 requests.

As Matt mentioned: dont put the socket response handlers inside the callback, because you loose the reference to them and a new handler is attached for every request.

var http = require('http');
var net = require('net');
var HOST = '192.168.1.254';
var PORT = 8888;
var client = new net.Socket();
var url   = require('url');

http.createServer(function (req, res) {
  console.log(url.parse(req.url));
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n', function(){

        console.log('Connected To: ' + HOST + ':' + PORT);

  });
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
pfried
  • 5,000
  • 2
  • 38
  • 71
0

The client.on() calls shouldn't be inside the res.end() callback - that's causing the event handler to be attached multiple times, meaning that every time data is received, it's getting called multiple times.

Try this instead:

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n', function(){
      client.connect(PORT, HOST, function() {
        console.log('Connected To: ' + HOST + ':' + PORT);
        client.write('0109001' + "\n");
      });   
  });
}).listen(1337, '127.0.0.1');

// Add a 'data' event handler for the client socket
// data is what the server sent to this socket
client.on('data', function(data) {
    console.log('The Data: ' + data);
    client.destroy();
});

// Add a 'close' event handler for the client socket
client.on('close', function() {
    console.log('Connection closed');
});
Matt Browne
  • 12,169
  • 4
  • 59
  • 75
  • 2
    are you sure this is the problem? I think it is attached for every request. It is a zombie handler, but i dont think it is causing the problem of 2 requests on the other server the socket connects to – pfried Mar 06 '13 at 08:01
  • @pfried I think you may be right...now that you mention it, it seems that the socket is only being written to by the client.write() line – Matt Browne Mar 06 '13 at 08:09
  • I forgot it that the http header from browser include the favicon request :) Thanks for pointing me out to place the event handler outside of the callback – BigBoss Mar 06 '13 at 11:54