Sunday, 18 May 2025

Establish Socket Connection Between AWS [EC2 UBUNTU] And Computer

  


AWS EC2 Ubuntu Setup :-

https://www.youtube.com/watch?v=uWFKq4KuRb0

Download PuTTY and FileZilla

https://www.putty.org/

https://filezilla-project.org/


Subscribe my Channel:-

http://www.youtube.com/user/SanketRooney?sub_confirmation=1

Gaming Channel:-

https://www.youtube.com/@unpoggaming9532

Gaming Instagram:- (@unpog.gaming)

https://www.instagram.com/unpog.gaming/

Facebook Page:-

https://www.facebook.com/UnpossibleNS

Twitter Account:-

https://twitter.com/UnpossiblePOG

Blog:-

https://unpossiblepog.blogspot.com/

Website:-

https://unpossiblepog.com/

========================

Code :

Server.java

import java.io.*;

import java.net.*;

public class Server {

public static void main(String[] args) {

int port = 5000;

try (ServerSocket serverSocket = new ServerSocket(port)) {

System.out.println("Server started. Waiting for client...");

Socket socket = serverSocket.accept();

System.out.println("Client connected: " + socket.getInetAddress());

// Input from client

BufferedReader clientInput = new BufferedReader(new InputStreamReader(socket.getInputStream()));

// Output to client

PrintWriter clientOutput = new PrintWriter(socket.getOutputStream(), true);

// Input from server console

BufferedReader serverConsole = new BufferedReader(new InputStreamReader(System.in));

// Create a thread to receive messages from the client

Thread receiveThread = new Thread(() -> {

String messageFromClient;

try {

while ((messageFromClient = clientInput.readLine()) != null) {

System.out.println("Client: " + messageFromClient);

}

} catch (IOException e) {

System.out.println("Connection closed.");

}

});

receiveThread.start();

// Main thread: send messages from server to client

String messageToClient;

while ((messageToClient = serverConsole.readLine()) != null) {

clientOutput.println(messageToClient);

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

-------------------------------------------

Client.java

import java.io.*;

import java.net.*;

public class Client {

public static void main(String[] args) {

String host = "_________"; // Replace with your EC2 IP

int port = 5000;

try (Socket socket = new Socket(host, port)) {

System.out.println("Connected to server.");

// Input from server

BufferedReader serverInput = new BufferedReader(new InputStreamReader(socket.getInputStream()));

// Output to server

PrintWriter serverOutput = new PrintWriter(socket.getOutputStream(), true);

// Input from client console

BufferedReader clientConsole = new BufferedReader(new InputStreamReader(System.in));

// Thread to receive messages from server

Thread receiveThread = new Thread(() -> {

String messageFromServer;

try {

while ((messageFromServer = serverInput.readLine()) != null) {

System.out.println("Server: " + messageFromServer);

}

} catch (IOException e) {

System.out.println("Connection closed.");

}

});

receiveThread.start();

// Main thread: send messages from client to server

String messageToServer;

while ((messageToServer = clientConsole.readLine()) != null) {

serverOutput.println(messageToServer);

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

========================

I was wondering if we could communicate between AWS operating system and PC using command prompt. Found out that this is possible. All we need to do is little bit of setup. The main advantage of using sockets is that once the connection is established, showing the message sent from server to client or vice-versa will not require any triggers like auto-refresh or button click etc. Sockets will handle the rest. Once sockets receives message, we can make program do all sorts of things like like creating new files, deleting files, showing files list etc. Any ways, our task is to establish connection using java sockets and that’s what we are going to do in this video.

In setup, our server will be AWS cloud server more specifically Elastic Compute Cloud, in short EC2. I installed ubuntu operating system in it. Remember that EC2 does not provide user interface to interact with files inside OS, so we shall use commands instead. Here, we shall not do a server setup from scratch. The server setup tutorial links are given in the description.

For client device, we are going to use windows machine which has Java already installed it, so make sure that you have that as well.

First, we shall need a PPK file. With that file, we can connect AWS server’s file system via FILEZILLA and also to run commands using PuTTY terminal. So, make sure that you installed them on you computer as well.

Let’s create KEY PAIR to download ppk file. Follow my steps.

[

From your AWS dashboard, click on EC2 -> Instances -> Key Pairs -> Create key pair

]

Make sure that pair type is RSA and file format is .ppk. Give any random name.

Once created, file will be downloaded automatically.

I already created key pair, so I am going to use that.

Next part is to enable port number 5000.

To create it, follow my steps.

[

Go to your EC2 Console

Select your instance → Networking → Security Groups

Click Inbound rules → Edit

Add a rule:

Type: Custom TCP

Port: 5000 (or any port you use)

Source: Your IP (or 0.0.0.0/0 for testing, but this is insecure)

]

Since I am not sure which group allows access, I am gonna create it in both.

Let’s connect ubuntu server using command prompt. To do that, open PuTTY.

Add IP Address, put 22 as a port number.

Select SSH radio button.

From the left side category, inside connection, go to select SSH, Auth, Credentials.

Select PPK file.

And click on open.

[How to know if ubuntu]

First we shall install Java.

sudo apt install openjdk-17-jdk.

To connect via FTP, use same credentials that we used for connecting server via putty.

Reach to var/www/ and create a folder where we are going to upload java file.

Let’s upload Server.java file. Code link is in the description.

Let’s go inside java-code folder.

Because the folder does not have permission, java file is no uploading. So reach to parent folder via putty terminal give edit permission to java-code folder.

Now, our server is ready.

I created a Client.java file. Code link is in the description.

Set AWS’s public IP as a value of String host variable.

In command prompt of client which is a windows system, we shall reach to a location where Client.java file is present.

Lets run client script.

And as you can see, whatever we type in command prompt from client reaches to server and vice-versa.