Latest Updates

UDP Chatting App Using Socket Programming


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.



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



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




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

  1. IP address family: ipv4,ipv6
  2. socket type: UDP, TCP

we will be using ipv4 its name is AF_INET and socket is UDP and name is SOCKET_DGRAM




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.



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



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.



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