Chat Program | Socket Programming

Monil Goyal
3 min readDec 28, 2020

I have created a chat Program using python language in which two servers interact with each other using the UDP protocol.

UDP Protocol

User Datagram Protocol is a connectionless protocol that does not acknowledge the data packet has reached or not to the destination. So it is also known as the non-reliable protocol.

Socket Programming

Socket Programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while other socket reaches out to the other to form a connection.

Socket module is used for creating a socket program in python.

To create a chatting Program, Both servers should able to send and receive the data packet at the same instant. multi-threading helps us to achieve this feature.

Multi-Threading In Python

Multi-threading is the ability of CPU to provide multi-threads to execute the program concurrently. Means if two commands are present in the program then they will be executed at the same time independently. This approach differs from multiprocessing.

Python has threading module to enable multi-thread feature.

Writing Code

→ Import socket module and threading module.

→ Pass type of IP used by the server ( IPv4 → AF_INET ) and Protocol ( UDP → SOCK_DGRAM ) in socket function of socket module.

→ Bind the IP of server and port to be used.

→ Define functions to send and receive the data.

→ In receive function, change the data type of data from byte to string by decode() function before using it in further code.

→ In send function, change the data type of data from string to byte by encode() function before sending it.

→ Create threads of both the functions.

Start the service on both the servers

→ Check the services using the following command

netstat -unlp

Service is running on both the servers.

Check the App

Thankyou For Reading…

--

--

No responses yet