Skip to content

Instantly share code, notes, and snippets.

@cankush625
Created January 8, 2021 10:54
Show Gist options
  • Save cankush625/c8a9b202027d2046147cd819d2a79c1c to your computer and use it in GitHub Desktop.
Save cankush625/c8a9b202027d2046147cd819d2a79c1c to your computer and use it in GitHub Desktop.
import socket
import threading
# Using IPv4 and UDP protocol
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ip = input("Enter your IP address: ")
port = int(input("Enter your port: "))
# Creating socket
s.bind((ip, port))
recvIP = input("Enter the IP of the receiver: ")
recvPort = int(input("Enter the port of the receiver: "))
# Function for receiving messages
def receiveMessages():
while(True):
data = s.recvfrom(1024)
data = data[0].decode()
print(f'\nReceived message: {data}')
# Function for sending messages
def sendMessages():
while(True):
message = input("Enter the message: ")
s.sendto(message.encode(), (recvIP, recvPort))
receive = threading.Thread(target=receiveMessages)
send = threading.Thread(target=sendMessages)
receive.start()
send.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment