UDP Chatting App Using Socket Programming
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCQZdqaRcIkQf9laxCJvf1Mbb6AXZrBEBU25yAE4IYOB5jJaL6O8uZR7LBy2a7-nZcYhHS2kgQlVhCMIqdDtKGfQ9tSbA3D_Zqqmbtb2pZa6EZcr34VzbeHvJwfp0pWNYyhOYd7ybvfNY/s16000/User-Datagram-Protocol-3.png)
WHAT IS 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 the other socket reaches out to the other to form a connection. The server forms the listener socket while the client reaches out to the server.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgo1aNg7KAaFiw1dK8lbtn0yO9dS9SQD_8UFxOF1v04k2_eMurJRtJOsyqc0-DMHv5jGBpgVknutDKD2bj1Q0doSW8xa_Lyb8FGDmsjcnB1vxLXVKaUrpvGxtWZ9iSf6LyGkCx3VV9Ck4w/s16000/udp-client-server-overview.png)
Where is Socket Used?
A Socket is used in a client-server application framework. A server is a process that performs some functions on request from a client. Most of the application-level protocols like FTP, SMTP, and POP3 make use of sockets to establish connections between client and server and then for exchanging data.
WHAT IS UDP??
User Datagram Protocol (UDP) is a Transport Layer protocol. UDP is a part of the Internet Protocol suite, referred to as UDP/IP suite. Unlike TCP, it is an unreliable and connectionless protocol. So, there is no need to establish a connection prior to data transfer.
Applications of UDP:
- Used for simple request-response communication when the size of data is less and hence there is lesser concern about flow and error control.
- It is a suitable protocol for multicasting as UDP supports packet switching.
- UDP is used for some routing update protocols like RIP(Routing Information Protocol).
- Normally used for real-time applications which can not tolerate uneven delays between sections of a received message.
Following implementations uses UDP as a transport layer protocol:
- NTP (Network Time Protocol)
- DNS (Domain Name Service)
- BOOTP, DHCP.
- NNP (Network News Protocol)
- Quote of the day protocol
- TFTP, RTSP, RIP.
What is threading??
Threading in python is used to run multiple threads (tasks, function calls) at the same time. Python threads are used in cases where the execution of a task involves some waiting. One example would be interaction with a service hosted on another computer, such as a webserver
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGIkLexZ8fLAJ8WtaQNQQ7aFzhbPjYzL6AMSqy-wdhejlmKdIRkLcwIlGqOCgEofurTV-M5YyTjeJvWNm9ycBdiJZv2Jb-2phou3iQzEPtukMn_fbz_1VVBUSICqfaNrHgqfVu83W6r8I/s16000/multithreading-python-5.png)
Task Objective
🔅 Create your own Chat Servers, and establish a network to transfer data using Socket Programing by creating both Server and Client machines as Sender and Receiver both. Do this program using UDP data transfer protocol.
🔅 Use multi-threading concept to get and receive data parallelly from both the Server Sides. Observe the challenges that you face to achieve this using UDP.
you can watch my video in the last so you will get an idea of how it works.
Let’s Get Started
To get a better understanding of how Socket programming helps us in connecting with the remote server we will perform the above chat app Task. in this task we will create a client-side and serverside program to chat so it looks like the client is chatting with his server over the network.
Note: we are using python so you should have python installed
Step1. to perform this Task we need some libraries following are the list of libraries
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi75zG_W75VW9yBjs-he7iUxwXfzJvyWcI48DmAx57timGnYJPzDGeC-yzhw65Sp_EalxJdB-7hxOCSvdqWqiejc9eEYSl7zh8f09_Nn2zTFJtZQ9ijIbqe_c0cgL0DT7n77pyHh_Gixfg/s16000/image.png)
Step2. we need client and server IP and their port
client IP: we need this because we also have to receive responses from server to client or vice-versa.
client port: client also receiving the responses so we need to open a port for that. every program is running on some port.
server IP: we need this for sending msg from server to client or vice-versa.
server port: server also receiving the responses so we need to open a port for that. every program is running on some port.
Step3. create a socket to start chatting with the server.
to create a socket we have the following function. it takes two parameter
- IP address family: ipv4,ipv6
- socket type: UDP, TCP
we will be using ipv4 its name is AF_INET and socket is UDP and name is SOCKET_DGRAM
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSVeBw_YEDyqZjIJAujbwJ80fifL1XsN-4Pp1JZULjvxiE8-fh2cwqYwcZ2_xMoESqlU3UWtQfu6XqYtnrX_aI_B1Lv2rOLqvjRcWw2y7EwTMqqLsU2Z0Vm-p5-rgEHqhD2TuQdSd9BiU/s16000/image.png)
we will need the bind() function to bind the system address with the port so it can receive a response from the client to the server or vice versa and it will happen on both sides as we receiving responses from both sides.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdkCyo4HOjNzGKAAeCP-se9VBl-jQOmjsKiDBsyu3y4ABZzIix1bSlAOMV9DElhjK5zJ0a6d36iyQciETAAZ8zSf7xdP-jFhdGk6hr7fIrAGsRflqhZDOgwPUQm2T-VwvRlcEYFmoOebI/s16000/image.png)
Note:
In case of client-side IP will be client IP and port will be client port you can consider it as sender IP and port as well.
In case of server-side IP will be server IP and port will be server port you can consider it as receiver IP and port as well.
Now using the Following function we can send or receive responses from client and server
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0gmE_OqMDKb1i7CADmJuZF2wks7lUJX3URVCL4q8tNhpmxEKCfsOn7kPGOaJrbVNsDqTm0hWzBKsqChn3YlFNtif8mutt9EH-xkhNdbccLMI44L1dP7mhPA42kZo9qfWErPBr8smAAUI/s16000/image.png)
In-network data goes in form of a byte so we need to change it into a byte or we can do one thing we can encode it with encode() function it also works.
sendto() function will send message.
recvfrom() will receive a response from the network and it takes one parameter that is the size of data it is also in bytes ex. 10byte.
So at the current point of time, it will only do one task because the program always runs sequentially but if we have functions and they need to run parallel so we need to give some more functionality to it.
every program when run it becomes a process so here we have to run one main process and inside it two subprocesses we need.it can be solved by threading, thread will give us a logical processor to run it in parallel. putting it in an infinite loop so we can send and receive responses infinite times like chatting.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGTqJBR40LXv8eMUnk9ZnWHdTuUsB2p2ibUroWKCSNtUry5JH4rMfaTs3V02NepdOXMBEkDX-aOr_hhDDDi3FM3lCJvDIJuAYwuAGb5wqO_CC1W_b7Sc-Pvf8IjX-GaQNtU9fkpgMIjoA/s16000/image.png)
Finally, we have done with the Technical part now you just have to combine all this and run the program on both sides.
GitHub:
https://github.com/venkateshpensalwar/ARTH/tree/main/Networking
Conclusion:
So In this way, we can set up some basic chat kind of app with the help of Socket programming in python using UDP or TCP. instead of printing things you can pass the command as well to perform some operation on the server this also is useful in some internal troubleshooting.
Hope this blog is helpful to you!!!
No comments