Datagram is a type of wireless communication between two endpoints and it requires the ip address and port number to establish connection. Datagram uses UDP(User Datagram Protocol), it converts user data into small packets or chunks of data so that it can be sent over a network in a continuous manner. In UDP type of connection data packets have no records at the sender’s end, it just sends by the user then it’s upon receiver that it wants to accept data packets or not. UDP type of connection we use in video conferencing or in video calls.
Let’s learn how to send some data on localhost network and receive the same data as a receiver on the same machine to demonstrate the use of datagram.
To know the IP configurations of your computer, use this command on terminal:
Code #1: For sender’s end.
- Specify your IPv4 address in the place of UDP_IP and remember 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 sock object to call function
sendto(), then pass argument of tuple containing IP address and port number.
Step #2: At the receiver’s end.
Received Message: b'Geeksforgeeks'
- We need to specify the localhost IPaddress in
UDP_IPand use the socket.socket() function same as above.
- bind the parameter to the socket object so that anything receive at these port address will we catch at this end. In the loop, define the buffer size as 1024, As message is not big, it is sufficient buffer size.
- Important differences between Python 2.x and Python 3.x with examples
- Reading Python File-Like Objects from C | Python
- Python | Convert list to Python array
- Python | Index of Non-Zero elements in Python list
- Python | Merge Python key values to list
- Python | Add Logging to a Python Script
- Python | Sort Python Dictionaries by Key or Value
- Python | Add Logging to Python Libraries
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Visualizing O(n) using Python
- pow() in Python
- gcd() in Python
- SHA in Python
- max() and min() in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.