Published in


Create Your Own Echo Server in Java

Task: Create a program that reads and writes to a socket.

Goal: A server receives information from client and echoes it back.

Language: Java

Flow of execution:

  1. Open a socket
  2. Create an open input stream and an output stream to the socket.
  3. Read and write to stream.

Key terms: ServerSocket, Socket, Poll, accept method, PrintWriter, BufferedReader

Communication between the server and client


We have two class files to manage: and


Initially, the server is socketless. The ServerSocket class polls for incoming client connections.


Knowing the hostname and port number of the server that is listening, the client requests server connection. To identify itself to the server, the client binds to a local port number it will use during the connection.


Upon connection with the client, the server gets a new socket bound to the same local port and has a remote endpoint set to the address and port of the client.


Upon connection with server, a socket is created to be used to communicate with the server.


1. Opening a socket.

A connection is established on the server-side once the client connects to the server. So first we must establish parameters to input our servers port:

To accept an incoming request to the server you must make use of the accept method within the server class file.

2. Creating an open input stream and an output stream to the socket.

The two classes we will be utilizing here are PrintWriter and BufferedReader class within the server class file.

Using the PrintWriter class the statement gets the output stream as the object out.

The same concept is used for the BufferedReaderclass, but instead we get an input stream for the object named in.

3. Reading and writing to stream.

Afterwards, we must have a way to insert the parameters for our hostname/IP and port number within the client class file:

To get the client to be able to send messages and receive them back we can use the BufferedReaderand PrintWriter classes.

Final steps

We are almost finished. The execution is simple.

Open up your terminal and start up the server class file using your preferred port number:

Now the server is listening!

Next, open a new terminal and input your IP/hostname and the port number used for the server.

AND THAT’S IT! You should be able to send a message from the terminal and get an echo response immediately.

The full code is available on my Github.

Thank you for reading. Was this article helpful? Confusing? Leave a comment and let me know. Also, If you want to see more, join me on my tech journey by following me on Medium.




We empower and invest in diverse technology talent to increase representation for all people, and to create higher performing enterprise organizations.

Recommended from Medium

“Funslingers” Devblog #2 | Instantiating & Destroying Gameobjects in Unity

Getting Started with Xero .NET SDK


Announcing swap tasks in Zoba Move

Understanding APIs using JSONplaceholder

Acquia Dev Desktop: Drupal install causes WSOD

Two Simple Ways to Count Backwards in Python

My journey becoming a Unity game developer: 2.5D Infinite Runner-Updating the Coin System

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
James Joseph

James Joseph

More from Medium

XML External Entity Attacks in JAVA:

Java Libraries

Building a HashSet in Java

Modern Java scripts that every Software Engineer must know