Created a ChatApp using the concept of Socket Programming (UDP Protocol) & Multi-Threading

Neeraj Singh Negi
2 min readJan 2, 2021

Create a UDP python program to Communicate between two systems using the UDP (User Datagram Protocol).

Socket Programming

Not all the program has the capacity to get the data from the network. It is decided by the developer of the module or library.

A Socket is basically the combination of the IP and the port no and Port No. is exactly equal to the program and it is identified uniquely.

Ip : Port no.

Process interacts with the network.

Network Packet

Rules

  1. Transmision Control Protocol (TCP)
  2. User Datagram Protocol (UDP)
Photo by Jordan Harrison on Unsplash

TCP

It is connection-oriented that always acknowledges or handshake (wait for the connection) the receiver due to this behavior it is slow.

If there is something wrong happens to the packet or receiver end then it resends the packet.

It is reliable and it is used when there is every packet (data) is important.

Example

SMPT, HTTP, FTP, etc.

UDP

It is connectionless and keeps on sending packets without knowing there is a receiver or not due to this behavior it is fast as compared to the TCP.

Non-reliable might lose packet (data) if there is something wrong happens to the packet or receiver end.

Example

Live stream, live videocall, live audio call, etc.

Code

import socket
import threading
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)my_ip = "192.168.43.101"
my_port = 1234
s.bind((my_ip, my_port))def get_data():
while True:
x = s.recvfrom(1024)
print(x[0].decode())
def send_data():
while True:
server_ip = "192.168.43.162"
server_port = 1234
msg = input()
msg = "Neeraj : " + msg
s.sendto(msg.encode(), (server_ip, server_port))
get_data = threading.Thread(target = get_data)
send_data = threading.Thread(target = send_data)
get_data.start()
send_data.start()

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Neeraj Singh Negi
Neeraj Singh Negi

Written by Neeraj Singh Negi

AWS | Openstack | GCP | Ansible | Redhat 8 Linux | Docker | Jenkins | Kubernetes | Hadoop | Python | Machine Learning

No responses yet

Write a response