简体   繁体   English

客户端如何使用 EC2 连接到服务器

[英]How does the client connect to the server with EC2

I have just put my socket server on Amazon EC2 and the server is up and running.我刚刚将我的套接字服务器放在 Amazon EC2 上,服务器已启动并正在运行。 The port for the server socket is ss = new ServerSocket(30001);服务器套接字的端口是ss = new ServerSocket(30001); . . What do I set the socket port inside my client class to?我将客户端 class 内的套接字端口设置为什么? It is currently on local host.它目前在本地主机上。 socket = new Socket("localhost", 30001); The amazon EC2 address is亚马逊 EC2 地址是
ec2-user@ec2-34-253-76-28.eu-west-1.compute.amazonaws.com Do I just replace localhost with this? ec2-user@ec2-34-253-76-28.eu-west-1.compute.amazonaws.com我只是用这个替换 localhost 吗?

If the client is remote (not on the same host as the server) then, yes, use the host's DNS name or public IP address.如果客户端是远程的(与服务器不在同一主机上),那么,是的,使用主机的 DNS 名称或公共 IP 地址。

You will also have to allow inbound connections to the EC2 instance hosting your server application.您还必须允许与托管您的服务器应用程序的 EC2 实例的入站连接。 Ensure that port 30001 is open for ingress to your client's public IP address (or to the world by indicating 0.0.0.0/0 as the source CIDR).确保端口 30001 对您的客户端的公共 IP 地址(或通过将 0.0.0.0/0 指示为源 CIDR 向世界)开放。 You do this in AWS via Security Groups .您可以通过Security Groups在 AWS 中执行此操作。

If you expose your server to the world, then you should implement (at least) some form of authentication for your clients.如果您将您的服务器暴露给世界,那么您应该(至少)为您的客户端实施某种形式的身份验证。

A few things to check, if your client cannot connect:如果您的客户端无法连接,请检查几件事:

  • Is your server socket bound to 0.0.0.0 (or the public IP associated with the EC2 instance)?您的服务器套接字是否绑定到 0.0.0.0(或与 EC2 实例关联的公共 IP)?
  • Is your server app running?您的服务器应用程序正在运行吗?
  • Does netstat show your server app listening on port 30001? netstat 是否显示您的服务器应用程序正在侦听端口 30001?
  • Did you add a security group to the EC2 instance and add an ingress rule allowing inbound traffic from your client IP (or the world) to port 30001您是否向 EC2 实例添加了一个安全组并添加了一个入口规则,允许从您的客户端 IP(或世界)到端口 30001 的入站流量
  • Is the client running on a network (eg corporate) that blocks outbound port 30001客户端是否在阻止出站端口 30001 的网络(例如公司)上运行

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

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