Enable Dark Mode!
datagram-in-python.jpg
By: Ruksana P

Datagram In Python

Technical

Data transfer can occur through various methods and protocols in the networking and communication worlds. One of these methods is datagram communication. In this blog, we check out what datagrams are and how we can implement datagrams in Python.

What is Datagram used for?

A datagram is a type of wireless communication between two endpoints. It requires the IP address and port number to establish the connection. The two endpoints are the sender and the receiver. It uses the User Datagram Protocol (UDP), which converts data into chunks or small packets of data, so that it can be sent over a network in a continuous manner. Data packets have no records at the sender’s end; the user just sends them, and then it’s up to the receiver whether it wants to accept data packets or not.

Senders Program:

This Python program uses the socket module to create the sender’s program.
import socket  
UDP_IP = "localhost"
UDP_PORT = 8080
MESSAGE = "GeeksforGeeks"
print ("message:", MESSAGE)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(bytes(MESSAGE, "utf-8"), (UDP_IP, UDP_PORT))
For this, we need to import the socket first. Declare the variable for the IP address and port. Specify the IPV4 address in the UDP_IP variable. Port number 8080 is your localhost port number, so you need to specify the same. socket.socket() uses AF_INET in order to use the IPv4 address of your system, and SOCK_DGRAM is a type of protocol that we are using for communication. Use the sock object to call function sendto(), then pass an argument of a tuple containing the IP address and port number.

Receiver Program:

This Python program uses the socket module to create the receiver’s program.
import socket
UDP_IP = "localhost"
UDP_PORT = 8080  
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))  
while True:
    # buffer size is 1024 bytes
    data, addr = sock.recvfrom(1024) 
    print ("Received message:", data)
For this, import the socket first. We need to specify the local host IP address in UDP_IP. Use the socket.socket() function same as above. Bind the parameter to the socket object so that anything received at these port addresses will be caught at this end. In the loop, define the buffer size as 1024; as the message is not big, it is a sufficient buffer size.

Use Cases:

Datagram communication is particularly useful in scenarios where real-time or low-latency data transfer is required. Some common use cases include:
Voice and Video Streaming: Datagrams are ideal for applications like VoIP and video conferencing, where low-latency transmission of audio and video is crucial.
Online Gaming: Online multiplayer games often use datagrams to transmit game state updates rapidly.
Network monitoring: Datagrams are valuable in network monitoring tools, where quick data transmission and minimal overhead are essential.
IoT Devices: Internet of Things (IoT) devices may use datagrams for efficient data exchange when reliability isn’t the primary concern.

Conclusion:

In this blog, we’ve delved into the world of datagram communication in Python. We’ve explored what datagrams are, how to send and receive them, and some common use cases for this approach. Datagram communication, facilitated by the socket module, is a powerful tool in your networking arsenal when speed and efficiency matter. So, the next time you’re building a real-time application or monitoring a network, consider giving datagrams a try!


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment

 


whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

London

Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message