Develop a TCP Chat Room Using Python | Cybersecurity & Python Beginner Project

Published: 03 October 2025
on channel: Vathos Technologies
98
8

Welcome to Vathos Technologies!
In this tutorial, you'll learn how to build a simple "TCP Chat Room" using "Python sockets". This project will help you understand how client-server communication works over TCP.

💡 What You'll Learn:
Basics of TCP/IP networking
Python socket programming
How to create a chat server
How multiple clients can join and chat in real time

📁 Project Type: Networking / Cybersecurity Project
👨‍💻 Level: Beginner-friendly

📌 Code + Demo Included
📢 Don’t forget to Like, Subscribe, and Turn on Notifications for more real-world cybersecurity tutorials!

WhatsApp/Call Us: +2348161842149

SERVER SCRIPT
import socket
import threading

Connection Data
host = '127.0.0.1'
port = 55555

Starting Server
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.listen()

Lists For Clients and Their Nicknames
clients = []
nicknames = []

Sending Messages To All Connected Clients
def broadcast(message):
for client in clients:
client.send(message)

Handling Messages From Clients
def handle(client):
while True:
try:
Broadcasting Messages
message = client.recv(1024)
broadcast(message)
except:
Removing And Closing Clients

index = clients.index(client)
clients.remove(client)
client.close()
nickname = nicknames[index]
broadcast('{} left!'.format(nickname).encode('ascii'))
nicknames.remove(nickname)
break

Receiving / Listening Function
def receive():
while True:
Accept Connection
client, address = server.accept()
print("Connected with {}".format(str(address)))

Request And Store Nickname
client.send('NICK'.encode('ascii'))
nickname = client.recv(1024).decode('ascii')
nicknames.append(nickname)
clients.append(client)

Print And Broadcast Nickname
print("Nickname is {}".format(nickname))
broadcast("{} joined!".format(nickname).encode('ascii'))
client.send('Connected to server!'.encode('ascii'))

Start Handling Thread For Client
thread = threading.Thread(target=handle, args=(client,))
thread.start()

print("server is listening...")
receive()

CLIENT SCRIPT
import socket
import threading

Choosing Nickname
nickname = input("Choose your nickname: ")

Connecting To Server
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('127.0.0.1', 55555))

Listening to Server and Sending Nickname
def receive():
while True:
try:
Receive Message From Server
If 'NICK' Send Nickname
message = client.recv(1024).decode('ascii')
if message == 'NICK':
client.send(nickname.encode('ascii'))
else:
print(message)

except:
Close Connection When Error
print("An error occured!")
client.close()
break

Sending Messages To Server
def write():
while True:
message = '{}: {}'.format(nickname, input(''))
client.send(message.encode('ascii'))

Starting Threads For Listening And Writing
receive_thread = threading.Thread(target=receive)
receive_thread.start()

write_thread = threading.Thread(target=write)
write_thread.start()


On this page of the site you can watch the video online Develop a TCP Chat Room Using Python | Cybersecurity & Python Beginner Project with a duration of hours minute second in good quality, which was uploaded by the user Vathos Technologies 03 October 2025, share the link with friends and acquaintances, this video has already been watched 98 times on youtube and it was liked by 8 viewers. Enjoy your viewing!