Open specific port such as 8082 in Google Compute Engine VM instance

How to open specific port such as 8082 in Google Compute Engine ?

This tutorial guides you on how to open specific port such as 8082 in Google Compute Engine VM instance and test the connectivity on the opened port using Telnet.

Open specific port such as 8082 in Google Compute Engine

Before you begin, make sure that you create a compute engine VM instance and start.

If you don’t have any server application running on the compute engine VM instance which is listening to port 8082, you can use the following instructions to setup simple HTTP server.

First, connect to VM instance using SSH connect option in the Google Cloud Console.

Then copy the following code.

SimpleHTTPServer.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class SimpleHTTPServer {

    public static void main(String args[] ) throws IOException {

        ServerSocket server = new ServerSocket(8082);
        System.out.println("Listening for connection on port 8082 ...");

        while (true) {

            Socket clientSocket = server.accept();
            InputStreamReader isr =  new InputStreamReader(clientSocket.getInputStream());
            BufferedReader reader = new BufferedReader(isr);
            String line = reader.readLine();            
            while (!line.isEmpty()) {
                System.out.println(line);
                line = reader.readLine();
            }

        }
    }

}

Use vim editor and paste the code in your working directory

sneppets@instance-1:~/java$ pwd
/home/demo/java

sneppets@instance-1:~/java$ vi SimpleHTTPServer.java

Then compile the java code.

sneppets@instance-1:~/java$ javac SimpleHTTPServer.java

sneppets@instance-1:~/java$ ls
SimpleHTTPServer.class  SimpleHTTPServer.java

Run the java code. Therefore simple HTTP server will be started and it will listen for connections on port 8082.

sneppets@instance-1:~/java$ java SimpleHTTPServer

Listening for connection on port 8082 ....

Firewall Rule to open specific port in GCP

Once you have the Simple HTTP server setup ready up and running, listening for connection on port 8082.  Now, let’s see how to create a firewall rule to open specific port such as 8082 in Google Cloud Platform.

To create a firewall rule follow the below steps:

1: Go to Google Cloud Console.

2: Choose Networking -> VPC Networks -> Firewall from side menu.

3: Click “Create Firewall Rule” and specify the firewall rule details as shown in the following screenshot.

Open specific port such as 8082 in Google Compute Engine VM instance

 

4: Click Create.

That’s it firewall rule is created to open specific TCP port.

Test Connection to VM instance for a specific port

Now, let’ see how to test connection to VM instance for a specific port.

You can use Telnet client to test the connection to VM instance for a specific port as shown below.

To test from windows machine make sure that you enable telnet and try testing as shown below.

test open specific port 8082 vm instance

 

You can also test from Linux machine as shown below.

$ telnet 35.223.9.136 8082

Trying 35.223.9.136...
Connected to 35.223.9.136.
Escape character is '^]'.

That’s it. Hope it helped 🙂

References

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments