Chat Application using python3
Now a days chatting applications are so common and sometimes we wish that we have our own chatting application with our own custom features and look and feel. So I tried to make a CLI based chatting program in Python3 using the concept of socket programming.
What is socket programming??
The first question that arises in the mind a beginner is what are sockets and what is socket programming???
A network socket is an internal endpoint for sending or receiving data within a node on a computer network.
So now we can conclude that making such programs that control the network sockets is termed as socket programming. With socket programming we can control the network sockets and send data to other systems on the network to transfer data. The most common type of socket applications are client-server applications, where one side acts as the server and waits for connections from clients.
Getting hands dirty
Now we know what is socket programming and why is it used. Python provides a socket module to make such programs. This is the module we’ll use to make a client server chatting application.
The primary socket API functions and methods in this module are:
- socket() : used to create instance of the socket
- bind() : binds ip and port number with the socket object
- listen() : listens for the incoming request from the client
- accept() : accepts the incoming request from the client
- connect() : connect client to the server’s socket
- recv() : receives the data from the other end
- send() : sends the data to the other end
- close() : close the session between the client and server
In this client-server architecture,
- Server creates a socket and binds it with its IP and port number.
- It listens for the incoming request on the binded port.
- Client make a socket and connect it to the server’s IP and port. (3 way handshaking)
- Server accepts the client request and then the data transfer between server and client starts.
- When the data transfer is complete the connection made between client and server is closed. (connection termination)
Server Side [code]
import socket # import socket module
ip="192.168.43.92" # server's IP address
port=4545 # server's port dedicated for the program s = socket.socket() # create socket
s.bind((ip, port)) # bind the IP and port with the socket
s.listen() # listen for incoming requests on port 4545c, addr = s.accept() # accepts the incoming request
# + make client objectwhile True:
c_data = c.recv(1024).decode() # receive client data
print("[msg]> "+c_data) # print client data
if c_data == 'exit':
break
c.send(input("[you]> ").encode()) # sends the server datac.close() # close client session
s.close() # close socket
Client Side [code]
import socket # import socket modules=socket.socket() # create socket instances_ip = "192.168.43.92" # server's ip address
s_ port = 4545 # server's port number
s.connect((s_ip, s_port)) # connecting to server's socketwhile True:
s.send(input("[you]> ").encode()) # send data to server
s_data = s.recv(1024).decode() # recv data from server
if s_data == 'exit':
break
print("[msg]> "+s_data) # print server datas.close() # close socket
These codes provide the basic functionality for a chatting application. This code can modified in order to provide more functionalities, use-cases, UI, etc as per individual’s requirement.
Note: IP address and port number used in the above code are facts of my machine, you have to replace them with the facts of your machine.
- To get IP you can use “ipconfig” command for windows and “ifconfig” command for linux.
- For port number you can use any free port of your system. To check the busy ports you can use “netstat” command with suitable options/flags.
- Both the devices (client and server) should be connected to each other (i.e. are on same network or ping-able).
- Both the code share same IP that belongs to the server. So you have to use the IP of the server machine.
That’s it for now. If you modify this for some other use case then I would love to know about it.