How does the client connect to the server with EC2

Viewed 261

I have just put my socket server on Amazon EC2 and the server is up and running. The port for the server socket is ss = new ServerSocket(30001);. What do I set the socket port inside my client class to? It is currently on local host. socket = new Socket("localhost", 30001); The amazon EC2 address is
ec2-user@ec2-34-253-76-28.eu-west-1.compute.amazonaws.com Do I just replace localhost with this?

1 Answers

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.

You will also have to allow inbound connections to the EC2 instance hosting your server application. 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). You do this in AWS via Security Groups.

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)?
  • Is your server app running?
  • Does netstat show your server app listening on port 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
  • Is the client running on a network (e.g. corporate) that blocks outbound port 30001
Related