简体   繁体   中英

Setting up simple Server/Client Socket for communication on AWS EC2

I am looking to set up a simple communication socket to send messages from my local machine (Windows) to my AWS EC2 instance via the command line. I already have EC2 setup and node installed. My struggle is determining which Port/Host to use for this communication. Please see the following:

server.js (running on AWS EC2):

var net = require('net');

var HOST = '127.0.0.1'; 
var PORT = 8080;

// Create a server instance, and chain the listen function to it
// The function passed to net.createServer() becomes the event handler for the 'connection' event
// The sock object the callback function receives UNIQUE for each connection
net.createServer(function(sock) {

    // We have a connection - a socket object is assigned to the connection automatically
    console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);

    // Add a 'data' event handler to this instance of socket
    sock.on('data', function(data) {

        console.log('DATA: ' + data);
        // Write the data back to the socket, the client will receive it as data from the server
        sock.write('You said: "' + data + '"');

    });

    // Add a 'close' event handler to this instance of socket
    sock.on('close', function(data) {
        //console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
    });

}).listen(PORT, HOST);

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

client.js (running on my local windows machine):

var net = require('net');

var HOST = '127.0.0.1'; 
var PORT = 8080;

var client = new net.Socket();
client.connect(PORT, HOST, function() {

    console.log('CONNECTED TO: ' + HOST + ':' + PORT);
    // Write a message to the socket as soon as the client is connected, the server will receive it as message from the client 
    client.write('I am Chuck Norris!');

});

// 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('DATA: ' + data);
    // Close the client socket completely
    client.destroy();

});

// Add a 'close' event handler for the client socket
client.on('close', function() {
    console.log('Connection closed');
});

Note, that I have setup my Security Group as follows: 在此输入图像描述

Note that when I run the code above, the EC2 output is: "Server listening on 127.0.0.1:8080"

However, the client.js running on my Windows machine has the following error: 在此输入图像描述

This simple example works when server.js and client.js are both run locally. Please provide any guidance to help, so that I can send simple messages between my Windows machine and my EC2 instance.

You will never be able to connect to anything that's listening on 127.0.0.1 from outside the machine. That's the loopback interface, only accessible from the machine itself... which would explain why it works locally.

You're seeing "connection refused" -- not because you can't access the EC2 instance -- but because you aren't trying to. You're trying to access the listener on your own local machine, where it isn't listening.

On the server, bind (listen) on host 0.0.0.0 and on the client, connect to the server's public IP address (or private, if you have a VPN).

And, as was mentioned in comments, you also need to allow TCP port 8080 in the inbound security group or you'll get a "connection timed out" error since packets will be discarded (not rejected, just dropped) at the edge of the EC2 network without a matching rule in the inbound security group.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM