By using AWS re:Post, you agree to the AWS re:Post Terms of Use

[Java] How can I send a message to my EC2 instance and then try to get a message back from it

0

I want to write a Java program that will send a String to an EC2 instance (Amazon Linux), the EC2 will then reply back with a String "Got message" that my Java program will print. What do I need to write on the client and server side to get this functionality?

1 Answer
2
Accepted Answer

To implement this functionality, you need to write Java code on both the client (your Java program) and the server (running on the EC2 instance). The EC2 instance acts as a server, and the Java program acts as a client. Here’s how to set it up using simple Java socket programming:

  1. Server Code (EC2 Instance) On the EC2 instance, create a server program that listens for connections, receives a message from the client, and responds with "Got message."
import java.io.*;
import java.net.*;

public class Server {
    public static void main(String[] args) {
        try (ServerSocket serverSocket = new ServerSocket(8080)) { // Bind the server socket to port 8080
            System.out.println("Server is running...");
            while (true) {
                try (Socket clientSocket = serverSocket.accept(); // Accept client connection
                     BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                     PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)) {

                    String message = in.readLine(); // Receive message from client
                    System.out.println("Received message: " + message);
                    
                    out.println("Got message"); // Respond to client
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  1. Client Code (Java Program) Now, create the client program, which connects to the server using the EC2 instance’s IP address and port, sends a message, and then waits for a response.
import java.io.*;
import java.net.*;

public class Client {
    public static void main(String[] args) {
        String serverAddress = "your-ec2-public-ip"; // EC2 instance's public IP address
        int port = 8080; // Server port number

        try (Socket socket = new Socket(serverAddress, port);
             PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {

            out.println("Hello EC2"); // Send message
            String response = in.readLine(); // Receive response
            System.out.println("Server response: " + response);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  1. Configuration Steps EC2 Security Group: Configure the EC2 instance’s security group to allow inbound traffic on port 8080 so the client can connect. Run the Server: Start the Server program on the EC2 instance. Run the Client: Run the Client program from your local machine. You should see the EC2 instance’s response printed out.
answered a month ago
profile picture
EXPERT
reviewed a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions