Simple Chat app using UDP protocol.

Anirudh Bambhania
4 min readJan 8, 2021

--

In this article we will create a simple chat app using UDP protocol and we will also be using socket programming and multi-threading concepts., here in this demo i have used python as a programming language.

  • User Datagram Protocol (UDP) is one of the core members of the Internet protocol suite. With UDP, computer applications can send messages, in this case referred to as datagrams, to other hosts on an (IP) network. UDP uses a simple connectionless communication model with a minimum of protocol mechanisms. UDP provides checksums for data integrity, and port numbers for addressing different functions at the source and destination of the datagram.
  • 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 programming alone won’t help us to achieve our goal because here in our application we want both send and receiver program to work in parallel with each other so that we can read the message sent by the user and also be able to send the message at the same time. To achieve this kind of parallelism we will be using multithreading.

  • In computer architecture, multithreading is the ability of a (CPU) to provide multiple threads of execution concurrently, supported by the operating system.

Program in linux OS

  • In python we have modules for both socket and multi-threading. Here in function b we have to give the IP and port number for the target node where we want to connect. I have used while loop so that the program won’t terminate until manually done. In “s” variable we have to give which Ip and protocol to be used by socket. AF_INET means all the IPv4 ip addresses and SOCK_DGRAM means UDP protocol. Now we need to bind our linux OS ip and the port number in the socket so that someone want to connect to our program it has to come to our ip and port this is called binding. We have to specify on which targets we want to implement multi-threading here we have to give the function name “a” and “b” and finally start the function.

Program in windows

In this program we have to give the Ip of the windows and the port number to be use. In the sendto() function we have to provide the ip and port number of the target node here in our case is the linux OS.

  • So now lets start our program in both linux and windows. After we start the program the cursor will be waiting for the input to send to the target node. So here first i have sent “ hello form windows !!!” message from windows OS to linux OS and we can see that the message is successfully delivered and than message “hello from linux :) ” is sent from linux OS to windows OS which is also successfully delivered.

→ form windows to linux

→ message received

→ from linux to windows

→ message received

Thank You

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

--

--