Skip to main content

Command Palette

Search for a command to run...

Building a Terminal-Based Chat Application with Python Sockets

Updated
6 min read
A
Iam a student interested in cybersecurity and more into analysis, reporting, documenting, auditing, policies, scanning and automation side of cybersecurity.

Introduction

Among the reasons for my interest in networking and communication technologies is an unusual one – the IRC-like chat programs featured in the films about hackers. Yes, the applications that run in the terminal window and allow communicating with each other by means of the text interface. Although the examples in the movies may be somewhat overstated, I have always found it interesting.

Now, after studying computer networks and communication protocols, I have created my own implementation using the sockets provided by the Python programming language.

This application was not meant to become a commercial solution; it was created more out of curiosity regarding how computers communicate with each other. In the end, however, it became a basic implementation of the terminal chat program allowing multiple users to send each other messages simultaneously.


Where the Idea Started

My love for Python sockets actually goes back much further than this. During my school days, I found the socket Python library documentation, which had some sample code examples.

Being curious, I pasted the code samples in a Python file and executed it to observe how changes were affecting the output. At the time, I did not completely know what was going on in the background. But I was just experimenting.

With time, as I gained knowledge about networking topics such as:

  • Client server structure

  • IP address

  • Ports

  • Communication protocols

  • Networking basics

I became quite clear about how sockets worked. It is at this point that I asked myself: "What if I could develop a chatting program using Python sockets within the Windows command prompt interface?"


Revisiting the Basics

Prior to starting with the coding for the program, I took some time to reflect on how the client server system operates.

Further, I looked into the socket programming and got an idea of different aspects of it like:

-Function

-Class

-Method

-Constants

-Socket operation

Instead of getting right into programming, I needed to know about how connection is formed and information travels between two systems.


Creating the First Version

The first iteration was quite simple.

It consisted of:

-A server application

-A client application

The server awaited connection requests while the client connected and communicated with the server. Rather than executing the applications within my IDE like Visual Studio Code, I opened a window in the Command Prompt on Windows and executed both files. For some reason, this added a layer of authenticity to the whole process as I recalled those early chat applications that served as inspiration for the whole project. Sending text between applications was quite satisfying.


Making It Feel Like a Real Chat Application

With communication up and running, I wanted to enhance the user experience. I altered the client code so that before doing anything else, the user is prompted to enter a username upon startup of the application. And after connecting:

-Users could send messages.

-Messages went to the server.

-The other clients were capable of receiving them.

-Every message also displayed the username of the user who sent it.

To test the new system, I opened up several Command Prompt windows on my computer. On one of the windows, I ran the server application. On the other windows, I ran the clients.

As I entered a message and hit enter within one client window, the message popped up within the other client window, including the name of the sender.

Although not very complicated, the ability to see two terminal windows talk to each other in real-time was absolutely amazing.


Taking It Further

Now that the program had run successfully on one computer, I wanted to raise the bar. I thought that my next objective should be enabling two entirely separate computers to talk to each other. In theory, this seemed pretty easy:

-Host the server on one computer.

-Have client connections on a different computer.

-Communicate through the network.

In reality, it did not seem to be as simple as I thought. I tried the following:

-IP addresses other than mine

-Several computers from the lab

-The use of VPN

But, I experienced problems related to the network. For instance, the computers I was working on did not have a proper network configuration for the required communication protocol, while some other computers were using different subnets.

Additionally, it was difficult for me to properly configure the VPN network. Finally, after several tries, I could not make the program work in the manner I desired. I thus stopped at this point and moved on to something else.


An Unexpectedly Fun Experience

Reflecting on it now, it seems that this project is not about making a chat program after all. It was more about experimentation.

Actually, I got into it one day when attending college lab classes along with my friend. We got this idea, began coding, tried various IP addresses on different computers in the lab, and saw if we could make our programs communicate with each other.

Sometimes it worked. Sometimes it did not. But the experience was fun regardless.

It makes me realize that learning can come from things outside of organized tutorials and class lessons sometimes. Sometimes it just comes out of nowhere and from the simple question: "What if I try this?"


What I Learned

Though the task was quite straightforward, it allowed me to learn a lot about:

-socket programming using Python

-client-server paradigm

-networking concepts

-IP address and port numbers

-transmission of messages

-network problems and their resolution

-studying from the manuals and documentation.

Perhaps even more important, it showed me the importance of experimentation in learning.


Final Thoughts

This program was never intended to be a major software undertaking. It started off as an interest-based exploration of network communications based on the design of terminal based chat programs.

Even though I could not fulfill my objective of creating a network for communication between several computers, it was still useful in understanding networking concepts.

Not all the most interesting projects are necessarily large ones; sometimes all you need is a good starting point and some curiosity.