1

I am developing a Client-Server application with several other programmers, in Java. At this point in time I do not want to be running the code locally. I want to be able to connect to the Server from any machine.

I wrote a test server and test client, just to make sure that things are working properly. But they are not. I am using Amazon AWS EC2 Linux that comes with Java. I am able to compile and run my Server after I SSH into the EC2, but the Client on my local disk is just not connecting. Here is the code.

// Code found online (https://cs.lmu.edu/~ray/notes/javanetexamples/) import java.io.IOException; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; public class TestServer { public static void main(String[] args) throws Exception { try (ServerSocket listener = new ServerSocket(50000)) { System.out.println("The capitalization server is running..."); System.out.println(listener.getInetAddress()); ExecutorService pool = Executors.newFixedThreadPool(20); while (true) { pool.execute(new Capitalizer(listener.accept())); } } } private static class Capitalizer implements Runnable { private Socket socket; Capitalizer(Socket socket) { this.socket = socket; } @Override public void run() { System.out.println("Connected: " + socket); try { Scanner in = new Scanner(socket.getInputStream()); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); while (in.hasNextLine()) { out.println(in.nextLine().toUpperCase()); } } catch (Exception e) { System.out.println("Error:" + socket); } finally { try { socket.close(); } catch (IOException e) {} System.out.println("Closed: " + socket); } } } } 
// Code found online (https://cs.lmu.edu/~ray/notes/javanetexamples/) import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.util.Scanner; public class TestClient { public static void main(String[] args) throws Exception { try (Socket socket = new Socket("ADDRESS HERE", 50000)) { System.out.println("Enter lines of text then Ctrl+D or Ctrl+C to quit"); Scanner scanner = new Scanner(System.in); Scanner in = new Scanner(socket.getInputStream()); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); while (scanner.hasNextLine()) { out.println(scanner.nextLine()); System.out.println(in.nextLine()); } } } } 

In place of "ADDRESS HERE" in the Client, I have tried the private IP and public IP of my Amazon EC2 instance. I have also tried the public DNS name. Nothing seems to work. There is just no connection from the Client to the Server. In fact, "Enter lines of text then Ctrl+D or Ctrl+C to quit" never prints.

All help is appreciated. Thank you.

2
  • 1
    You would need to use the public IP or public DNS. This is most likely a firewall issue. What port is the server running on? Have you opened that port in the AWS Security Group assigned to the EC2 instance? Commented Nov 20, 2019 at 21:50
  • Oops, I just found @MarkB posted related comment some seconds before I posted the answer with similar idea :-) Commented Nov 20, 2019 at 21:53

2 Answers 2

2

Allow your IP address to send request to the EC2. For this, you need to go to your Security Group and add your IP there. Follow these steps-

  1. GO to your AWS console.
  2. Click on EC2, then under Resources you will find Security Groups.
  3. Select your security group.
  4. Follow the steps in the given image.

steps to follow

Sign up to request clarification or add additional context in comments.

Comments

1

Since you're able to connect to EC2 instance via SSH, your Security Group allows this.
Now you need to allow requests from the client in this Security Group. You will either need to provide a concrete IP, IP range or allow all IPs (not recommended) in the group.
You can find how to do this here.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.