Client-Server Messaging Program using Socket Programming and UDP Protocol in Python
In day to day development, we use multiple client-server programs like webservers or mail servers, ssh, telnet, or other servers. But we rarely know how they work behind the scene. These servers use sockets behind the scene to make the connection and packet transfer.
One of the easier protocols to understand socket programming is the UDP protocol. So, in this article, I will show you how to implement a client-server program for message transfer using UDP protocol. And through the development of this program, you will understand the concept of socket programming.
First of all, let’s understand the requirement.
I want to develop a program that will allow me to send messages in text format.
There will be one server and one client. Both server and client can be able to communicate with each other.
That means I have to build a two-way communication program.
Before moving towards implementation let’s understand some terminologies in socket programming:
Every program on the internet is identified by an IP and port combination. This is known as a socket. And as we are using the socket in the program, it is known as socket programming.
The socket is consists of IP and Port number.
Port number is a unique number provided to a running program or process, on that number outside world can connect.
The process of attaching a port number to the process(running program) is known as binding.
For sending the data through a network, we required some rules. These rules are known as protocols. There are two major protocols available for sending data, viz, TCP and UDP. Out of which, I will use the UDP protocol for developing this program.
As you know the basic terms in socket programming till now, let’s move towards the implementation of the program.
Implementing a client-server program for messaging
Python provides a socket library for dealing with socket programming. So, first of all, I will import the socket library in the program.
Now, we have to create a socket object. For this socket, the library has one function named socket. This function requires two arguments. One is the address family and the second is the socket type.
I will use the IPv4 address family that we can get by socket.AF_INET and UDP protocol that we can get by socket.SOCK_DGRAM.
Assign this socket object to the variable s.
The next step is to create a socket. For creating sockets, we required IP and Port on which the program should connect to the outer world.
We will accept the IP and Port from the sender and store them in the variables IP and port, respectively.
After accepting the IP and Port, bind them to create a socket.
As the socket is created successfully, we are ready to transfer the data over the internet.
For transferring the data, we required the IP of the receiver and the port number through which we can connect to the receiver program.
Accept the receiver IP and receiver Port number and store it in the variables recvIP and recvPort, respectively.
After that, I will write two functions. The function receiveMessages() will receive the messages and prints them. And the function sendMessages() will prompt for a message and after we enter the message, it will send that message to the receiver.
The receiveMessage function uses the recvfrom() function provided by UDP for receiving the data. The recvfrom function accepts the buffer size as an argument.
The sendMessage function uses sendto() function provided by UDP for sending the data. The sendto function accepts the data to send in the bytes form as first argument and the second argument is a tuple of IP and port number of the receiver.
But, as discussed in this article earlier, I want to access both sendMessage and receiveMessage features concurrently. For using two functions concurrently, we have to make use of the multi-threading concept.
To deal with multi-threading, python provides a threading library. Import that library in the program.
Create a thread using threading.Thread() function that will require a named argument target as a function name to create a thread for.
Finally, start the threads.
Both the functions are now running parallelly and we can test the program by sending messages.
Final Program code:
Running the socket program in client and server
The program that I had created is created in such a way that the same program can be executed on the server and client.
I will place the same program on the client and server and execute both programs.
Now, I will send the message to either the client or server and go to the other machine and check if the message is received.
Do this for both client and server, simultaneously, and check if you are receiving messages and able to send messages to each other.
We can see that we are able to send and receive messages at the same time.
- The communication through this program is not reliable. As it uses UDP protocol, if the packet is dropped along the way for any reason, the receiver will not able to get that packet.
- There is no mechanism provided in the UDP protocol for getting to know if the packet is lost.
- This program will never check if it is connected to the receiver successfully or if the receiver is alive before transferring the packets.
You can develop this idea further to build your own client-server applications.
This program can be made more reliable and powerful using the protocols like TCP.