How to make an Echo server with Bash?
Last Updated :
08 Dec, 2023
An Echo server is a simple network interface/application that listens for any incoming connections and requests, and then echoes back any data it receives from clients. Creating such a server is a great way to learn about network programming and the basics of client-server communication. In this article, we will guide you through the process of building an echo server using Bash, a widely available and versatile scripting language.
Prerequisites:
- A Unix-based operating system (Linux or macOS), as Bash is native to these systems.
- A basic understanding of Bash scripting.
- Some familiarity with networking concepts.
Steps to create an Echo server:
Step 1: Create the server script
Open your favorite text editor and create a file called echo_server.sh. In this case, we are using nano editor, You can do this by running the following command:
nano echo_server.sh
In this step, we will create a bash script for our echo server. This script will set up the server to listen on a specific port and respond to incoming connections.
Creating script with nano editor
Now let's write the script in the editor, below is the code for the script:
#!/bin/bash
# Define the port on which the server will listen
PORT=12345
echo "Bash Echo Server is running and listening on port $PORT..."
# Start the server and listen for incoming connections
while true; do
(echo -ne "HTTP/1.1 200 OK\r\n"; cat) | nc -l -p $PORT
done
Writing script code
Script Breakdown:
- We provide a message to inform the user that the Bash Echo Server is running and listening on a specific port.
- The script configures the echo server to respond with a simple HTTP 200 OK message and echo the received data.
Step 2: Make the Script Executable
To run the script we just wrote, make it executable by the below chmod command, This command grants execute permissions to the script.
chmod +x echo_server.sh
- chmod: this command is used to change file permissions.
- +x: This is an argument that grants execute permissions to a specified file.
Making script executable
Step 3: Run the echo script
With the script ready and executable, you can now run your echo server. In the terminal, run the following command:
./echo_server.sh
The above command runs the echo_server.sh script. The ./ specifies that the script is located in the current directory.
Executing Server script
Our server is now running and listening for incoming connections on the mentioned port, now we need to test the server.
Step 4: Test the Echo Server
To test your echo server, you can use the telnet command or write a simple client script in Bash or any other programming language. Here's a simple Bash client script to test the server:
#!/bin/bash
# Define the server host and port
SERVER_HOST="localhost"
SERVER_PORT=12345
# Send a message to the server
echo "Sending a message to the Bash Echo Server..."
echo "Hello, Bash Echo Server!" | nc $SERVER_HOST $SERVER_PORT
# Wait for a brief moment
sleep 1
# Receive and print the server's response with a timeout
response=$(nc -l -p $SERVER_PORT -w 10) # Set a 10-second timeout
if [ -z "$response" ]; then
echo "No response received from the server within the timeout."
else
echo "Received a response from the server:"
echo "$response"
fi
Script Breakdown:
- SERVER_HOST="localhost": We define a variable SERVER_HOST and set it to "localhost," which means the client and server are running on the same machine.
- SERVER_PORT=12345: We define the variable SERVER_PORT and set it to the same port number used by the server.
- echo "Hello, Bash Echo Server!" | nc $SERVER_HOST $SERVER_PORT: This line sends a message to the server. The echo command outputs the message, and it is piped (|) to nc, which sends the message to the server using the specified host and port.
- nc -l -p $SERVER_PORT: This line opens another instance of nc to listen for the server's response, effectively acting as the client's receiver. The server will echo back the message, and it is printed to the terminal.
- We use the -w option with nc to set a timeout of 10 seconds for waiting to receive a response from the server. This means that if the server does not respond within 10 seconds, the client will exit with a message indicating that no response was received.
- We check whether the response variable is empty (indicating no response) or contains a response and then print the appropriate message.
Writing Client Script
You can save this code to a file called client.sh, make it executable, and run it. It will send a message to your echo server and print the server's response. to make and run the script follow steps 1 to 3 again.
Output
Explanation:
In the above Output, you can see that we have successfully made the connection.
- When we start the server script, it displays a message indicating that the server is running on port 12345 and is ready to accept any incoming connections.
- In the separate terminal, you can see we have our client script running, it's displaying a message that is about sending a message to the server.
- The client script sends the message "hello, bash echo server!" to the server on port 12345 using the nc command.
- The server receives the message from the client gives an acknowledgement of the message and echoes it as a response.
- The client script listens for a response from the server on the same port 12345 and receives an HTTP 200 OK message which tells us that the connection is successful.
Conclusion
In this article, we have learned how you can create a basic Echo Server, which listens on the specified port and address. You can always experiment and explore more by adding some more commands and actions in the script and enhancing your knowledge of bash scripting as well as the echo server. The above basic example demonstrates the principles of creating a network server and handling incoming connections.
Similar Reads
How to Make a Discord Server Public
Discord, initially popular among gamers, has expanded into a versatile platform that hosts a variety of interest groups, from educational clubs to hobbyist communities. Making your server public allows you to invite more participants, fostering diverse discussions and broadening the reach of your co
7 min read
How to add Bots to Discord Servers
Adding bots to your Discord server is an excellent way to enhance functionality, automate tasks, and engage your community. Whether youâre looking to moderate chats, play music, manage roles, or provide games and trivia, knowing how to add bots to Discord servers can transform your server into a dyn
8 min read
How to Download a File from a Server with SSH / SCP?
Secure Copy Protocol (SCP) is a secure file transfer protocol that allows you to transfer files between computers over a secure connection. It is based on the Secure Shell (SSH) protocol and is commonly used to transfer files between servers and local computers. One of the main benefits of using SCP
3 min read
How to Make a Discord Server - Beginner's Guide to Discord
How to Make a Discord Server - Quick Steps Open Discord and log in to your accountClick on the "+" icon on the left sidebarSelect "Create My Own" or choose a templateEnter your server name and upload an icon if desiredClick "Create" to finish.Creating a Discord server is an excellent way to build a
8 min read
How to Build a Simple Web Server with Node.js ?
Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. You need to remember that NodeJS is not a framework, and itâs not a programming language. Node.js is mostly used in server-side programming. In this article, we will discuss how to make
3 min read
How to Make a Discord Bot
If you have accessed Discord then you can notice that as soon as one can join any server, there is instant welcome to you. How is it possible? It is made possible by the help of BOTS which are made to perform specific tasks in the discord server. Building your own Discord bot is easier than you thin
11 min read
How to create a multi-chat server using UDP?
In this article, we will see the development of a Multi-Threaded UDP Chat Server-Client Network for Data Transfer in Linux. UDP is used for low latency and connectionless characteristics, the architecture consists of a server managing multiple clients through threading. This networked chat applicati
10 min read
Bash Script - How to use Command Line Arguments
In this article, let us see about Command Line Arguments usage in Bash script. Arguments are inputs that are necessary to process the flow. Instead of getting input from a shell program or assigning it to the program, the arguments are passed in the execution part. Positional Parameters Command-line
4 min read
How to make an HTTP GET request manually with netcat?
Netcat,also known as "nc", is a powerful Unix-networking utility that enables users to interact with network services through a command-line interface (CLI). It uses both TCP and UDP network protocols for communication and is designed to be a reliable back-end tool to instantly provide network conne
6 min read
How to Create Web Server On Packet Tracer?
A web server is like a computer that uses an HTTP (Hyper Text Transfer Protocol) and many other protocols. it responds when a client makes a request over the World Wide Web. The main work of the web server is to show website content that is processed, and stored, in the webserver to deliver the webp
2 min read