A simple Half-Duplex Chatting program in Python
Python is the most popular programming language for it’s readability and generally anything can be built using this pseudo-code alike language. It is best suited for scripting, Data Science, Machine learning, back-end web development, artificial intelligence and scientific researches.
In this blog article we will find out how to write a simplest half duplex TCP chat application written in python.
Before starting, let’s learn a little about socket programming.
What is Socket?
A socket is one of the most fundamental technologies of computer network programming. Sockets allow network software applications to communicate using standard mechanisms built into network hardware and operating systems. 1
A network socket is one endpoint in a communication flow between two programs running over a network. 2
Well, a definition is not enough. For practical implementation we will have to know about the
socket module of python. And obviously about the methods those will be called in from that module.
TCP Socket Stages
The following diagram 3 presents the stages of a TCP (connection-oriented) socket.
Now let’s look at the methods of python for socket programming.
Server Socket Methods
|s.bind()||binds address (hostname, port number pair) to socket|
|s.listen()||sets up and start TCP listener|
|s.accept()||passively accept TCP client connection, waiting until connection arrives|
Client Socket Methods
|s.connect()||initiates TCP server connection|
Common Socket Methods
|s.recv()||receives TCP message|
|s.send()||transmits TCP message|
Chat Application Code
Server side Script
# Server Side Script # Supports Python v3.* from socket import * server_port = 5000 server_socket = socket(AF_INET,SOCK_STREAM) server_socket.bind(('',server_port)) server_socket.listen(1) print ("Welcome: The server is now ready to receive") connection_socket, address = server_socket.accept() while True: sentence = connection_socket.recv(2048).decode() print('>> ',sentence) message = input(">> ") connection_socket.send(message.encode()) if(message == 'q'): connectionSocket.close()
Client Side Script
# Client Side Script # Supports Python v3.* from socket import * server_name = 'localhost' server_port = 5000 client_socket = socket(AF_INET, SOCK_STREAM) client_socket.connect((server_name,server_port)) while True: sentence = input(">> ") client_socket.send(sentence.encode()) message = client_socket.recv(2048) print (">> ", message.decode()) if(sentence == 'q'): client_socket.close()
Save the server side code in a file and name it as
server.py or anything as you wish. Also save the client side code in another file and name as
client.py. You can run the code in windows command prompt (cmd) by opening it in the directory you stored your script file. Then type
python server.pyand hit Enter. Running
client.py will require another cmd window. Type there as
python client.py and hit enter. You will get a message in the server side that a client is connected. Now, you can send and receive message from and to both ends.
An Overview of Socket Programming for Computer Networking- lifewire ↩
Network Socket definition - techtarget ↩
Introduction to Socket Programming - Communication Networks-University of Kansas ↩
Leave a comment