Basics of socket programming in python using UDP protocol

Socket :

Sockets allow communication between two different processes on the same or different machines, Just by using a combination of IP Address and Port Number

Socket = [ IP : port ]

Let's Understand how this only two thing can enable us to communicate:

  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

2. Port Number→ whenever any program is executed it becomes process, system gives each process a unique Id, but Only some programs that have capabilities to go over a network,this some program’s Unique process Id is known as Port Number

Process Id ==Port number

  • 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

Port Binding: To bind the Port Number(process ID of the program) and the reserve port number mentioned in the senders program

Reserve POrt: To make a socket we bind the IP Address to some random port no. which we mention in the sender's 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

Task Description

🔅 Create your own Chat Servers, and establish a network to transfer data using Socket Programming by creating both Server and Client machine as Sender and Receiver both, using UDP data transfer protocol.

Solution

Create 2 Programs sender and receiver in different systems but should connect to the Network

1. Receiver.py

In Receiver program need to mention the following things

  • import the socket module

import socket

  • 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)

protocol = socket.SOCK_DGRAM
ip_family = socket.AF_INET

s = socket.socket(ip_family,protocol)

  • 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)

ip=”192.168.43.76"
port= 1234
s.bind((ip,port))

  • use a function that receive the message 1024 kb is the max size of msg can received by this function

msg = s.recvfrom(1024)

print(msg)

when we run the receiver.py program it will wait for the sender to send the msg

  • to see the listening socket we have a cmd (only for UDP protocol)

netstat -unlp

2. senders .py

let's create the senders program

  • import the socket module and create an object of the socket

import socket
s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

  • 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

s.sendto(“hello”.encode() , ( “192.168.43.76” , 3456) )

Let's run the sender.py and see the output of the receiver end

Here is the msg received which contain the data and the socket of sender

TASK COMPLETED

*****************************Thank you************************

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Prometheus With AlertManager

Guys, the EMIT Epoch ecosystem uses a unique PoN mining mechanism, i.e.

How to build an Ethereum Remote Control (with Fluence Network): Part 2 — Connecting the UI

Imersão de Dados 03 Alura!

AUTOMATION WITH GITHUB ACTIONS

Building Network Effects for Your SAAS Product — Done

Unmarshal partners with AdLunam in building Reddit of Launchpads

Microservices as a concept

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akanksha Chhattri

Akanksha Chhattri

More from Medium

How I Monitor My 2021 Portfolio Performance Using Python

Dividends Discount Model (DDM) in Python

EV Stock Daily Cumulative Returns using Python Pandas

Introduction to Asyncio in Python