简体   繁体   English

设置简单的服务器/客户端套接字以在AWS EC2上进行通信

[英]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. 我希望设置一个简单的通信套接字,通过命令行将消息从本地机器(Windows)发送到我的AWS EC2实例。 I already have EC2 setup and node installed. 我已经安装了EC2设置和节点。 My struggle is determining which Port/Host to use for this communication. 我的斗争是确定用于此通信的端口/主机。 Please see the following: 请参阅以下内容:

server.js (running on AWS EC2): server.js(在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): client.js(在我的本地Windows机器上运行):

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" 请注意,当我运行上面的代码时,EC2输出为:“服务器侦听127.0.0.1:8080”

However, the client.js running on my Windows machine has the following error: 但是,在我的Windows机器上运行的client.js有以下错误: 在此输入图像描述

This simple example works when server.js and client.js are both run locally. 当server.js和client.js都在本地运行时,这个简单的示例有效。 Please provide any guidance to help, so that I can send simple messages between my Windows machine and my EC2 instance. 请提供任何帮助指导,以便我可以在Windows机器和EC2实例之间发送简单消息。

You will never be able to connect to anything that's listening on 127.0.0.1 from outside the machine. 您永远无法连接到从机器外部侦听127.0.0.1的任何内容。 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. 您看到“拒绝连接” - 不是因为您无法访问EC2实例 - 而是因为您没有尝试。 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). 在服务器上,在主机0.0.0.0上绑定(侦听),在客户端上,连接到服务器的公共IP地址(如果有VPN,则连接到私有IP地址)。

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. 并且,正如评论中所提到的,您还需要在入站安全组中允许TCP端口8080,否则您将收到“连接超时”错误,因为数据包将被丢弃(不会被拒绝,只是丢弃) EC2网络在入站安全组中没有匹配规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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