import sys
import os
import socket
import time
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(2)
if "--listen" in sys.argv:
print("WalkieTalkie Listener Turned On..")
server_address = ('', 9434)
sock.bind(server_address)
while True:
try:
data, address = sock.recvfrom(4096)
data = str(data.decode('UTF-8'))
print(str(address[0])+': '+data)
except KeyboardInterrupt:
print("Aborted!")
break
except socket.timeout as e:
time.sleep(1)
continue
except socket.error as e:
print(e)
sys.exit(1)
else:
print("WalkieTalkie Broadcast Turned On..")
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
server_address = ('255.255.255.255', 9434)
while True:
s=input("Enter message: ")
if s == "exit":
print("WalkieTalkie Broadcast Turning Off..")
sys.exit(1)
sock.sendto(s.encode(), server_address)