Building a Reverse Echo Server in Java

Building a Reverse Echo Server in Java

Socket programming is the backbone of network communication. Here, we will be building a reverse echo server using Java. The reverse echo server will read message that is sent by the client, reverse that message, and send them back to the client.

What is Socket Programming?

Socket Programming helps to establish communication between two end devices or nodes in a network. One node(Server) will listen on a port at some IP address, and another node(Client) will reach out to establish connection with the server. Once the server and client are connected then they can exchange data.

What is Reverse Echo Server?

If a client sends message to the server and the server responds back with the same message that is sent by the client, then this is known as Echo Server.

Reverse Echo Server means a client sends message to the server, then server will reverse that message and send it back to the client.

Implementation:

This project involves two main components:

  • Server - Server will receive the incoming messages and processes the data that comes from the Client.
  • Client - Client will connects to the server, sends the message and displays the server's response.

For making a client connect with the server and communicate, we basically need two things:

  1. IP address of server machine
  2. port number of server machine

Here, we will be using "Socket" class for developing client application and "ServerSocket" class for developing server application.

Both the client and server application will have input and output stream.

Server Code

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

public class Server {
    public static void main(String args[]) throws Exception
    {
        //creating object of server socket class
        ServerSocket ss = new ServerSocket(2000);

        //to accept the connection request from client
        Socket st = ss.accept();
        System.out.println("Connection Established!");

        //input stream
        BufferedReader br = new BufferedReader(new InputStreamReader(st.getInputStream()));

        //output stream
        PrintStream ps = new PrintStream(st.getOutputStream());

        String msg;
        StringBuilder sb;

        do{
            //read input from buffered reader
            msg = br.readLine();

            //now we need to convert the String to StringBuilder to reverse the message
            sb = new StringBuilder(msg);
            sb.reverse();

            //store the reverse string
            msg = sb.toString(); //toString method will convert StringBuilder class to String class

            //send reverse message back to client using printstream
            ps.println(msg);

        }while(!msg.equals("dne"));

        ss.close();
    }
}
        


Client Code

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

public class Client {
    public static void main(String args[]) throws Exception
    {
        //creating object of socket class
        Socket st = new Socket("localhost",2000);

        //input stream for keyboard
        BufferedReader keyb = new BufferedReader(new InputStreamReader(System.in));

        //input stream for server
        BufferedReader br = new BufferedReader(new InputStreamReader(st.getInputStream()));

        //output stream
        PrintStream ps = new PrintStream(st.getOutputStream());

        String msg;
        do{
            //reading input from keyboard
            msg = keyb.readLine();

            //send message to server
            ps.println(msg);

            //read the reverse message from the server
            msg = br.readLine();
            System.out.println("Reverse Message from Server: "+msg);
        }while(!msg.equals("dne"));

        st.close();
    }
}
        


Running the Code

  • Start Server: Server will be listening on port 2000.
  • Run the Client: Client will connect with the server and start sending messages to the server.


Article content


When we type "end" it will terminate the connection.

This project helps to understand the basics of Socket Programming in Java. Thank You :)

To view or add a comment, sign in

Explore content categories