3
votes

I created a virtual machine on Azure and defined a TCP endpoint. I then created a Node.js TCP server as follows:

var net = require('net');
var host = '127.0.0.1';
var port = 8000;
var times = 1;

net.createServer(function (socket) {
console.log('CONNECTED: ' + socket.remoteAddress + ':' + socket.remotePort);

socket.on('data', function (msg) {
    console.log("Message received is: " + msg);
    if (times == 1)
        socket.write("Server says HI");
    times++;
    executeStatement();
});

socket.on('close', function (data) {
    console.log('CLOSED: ' + socket.remoteAddress + ' ' + socket.remotePort);
});
}).listen(port, host);

console.log('System waiting at http://localhost:8000');

I am able to connect to the server while on the same host so two applications on different ports on the same machine can communicate bidirectionally. When I try connecting to the virtual machine through the public IP address and port number, it fails. I tried adding an inbound firewall rule on the virtual machine for the private port (8000) but that also doesn't make a difference. Please, can someone give me some ideas and/or links that help resolve this issue.

2
Have you created an endpoint, mapping some public port (e.g. 8000) to internal port 8000?David Makogon
Hi David. Yes I did. I managed to solve the problem. Will post the answer just now.Vinu

2 Answers

1
votes

Somehow, stating the host as 127.0.0.1 was causing the problem. I removed it and made the server listen on just the port alone i.e. listen(port). After doing this, I was able to detect that the port was open and then my program on the remote computer was able to communicate bidirectionally with the virtual machine.

0
votes

When you bind your HTTP server to the address 127.0.0.1, you are listening for incoming requests only on the local loopback interface. This means that any requests coming from other devices, even if on the same network, will be unable to reach your service.

Omitting the IP address will cause the service to listen on any available IP addresses on the host machine - see the Node.js docs. Note that some hosting providers will require your application to listen on specific IP address in order to be able to serve requests (i.e. Openshift).