Akanksha Chhattri

Mar 22, 2021

4 min read

Basics of socket programming in python using UDP protocol

Socket :

  1. IP Address: → An IP address is a unique address that identifies a device on the internet or a local network.
  • How IP Address help in the socket: so IP Address is the one who takes us to that system
  • let's compare it with a real-world example. To reach to a friends house we required their Proper Address, Same Way In Internet world each system have their Unique IP Address which act as the Address of the system
  • How Port Number help in socket → Port used to connect to the specific program which is running in our system
  • For Example, we Reached to friend's house, IN that house other family members also live. How the Each member can uniquely be identified? Using the name, same way in a name OR port no. help in the system to uniquely connect to a specific program
  • after binding the port we can check the socket in protocol-specific list of port IN our case we are using the UPD protocol
  • import the socket module
  • Have to create the object of the socket and in argument need to put the address family of IP AF_INET is only for ipv4 type and the Protocol SOCK_DGRAM is for User Datagram Protocol(UDP)
  • Then bind the IP Address and the random port which we are reserving for the socket and binding this port to the process ID (Port)
  • use a function that receive the message 1024 kb is the max size of msg can received by this function
  • to see the listening socket we have a cmd (only for UDP protocol)
  • import the socket module and create an object of the socket
  • To send the msg we have a sendto() function which able to send data over a network, this only accepts byte type to send, encode() help to convert a string into a byte
  • put receiver's IP and Port