I have a python code that is reliable implementation of UDP for messages. I am having problems with reliability, but can’t figure out what is wrong. I will post the listening part of the code and the sending part and other parts that are relevant.
def packeting(s):
L = []
sprime = s.split()
seqnum = 0
for k in sprime:
L.append((k, seqnum))
seqnum += 1
return L
seqnum_received = 0
def listen():
print("Listening: ... ")
L = []
seqnumwanted = 0
while True:
try:
messagerec, clientAddress = serverSocket1.recvfrom(2048)
msg = messagerec.decode()
message = eval(msg)
global seqnum_received
if type(message) == tuple:
seqnum_received = message[1]
if seqnum_received == seqnumwanted:
serverSocket1.sendto(str(seqnum_received + 1).encode(), ("127.0.0.1", 12000))
L.append(message[0])
seqnumwanted += 1
else:
serverSocket1.sendto(str(seqnumwanted).encode(), ("127.0.0.1", 12000))
elif type(message) == bool and message == True:
chatbox.insert(tk.END, "n||Peer: " + " ".join(L))
L = []
seqnumwanted = 0
seqnum_received = 0
elif type(message) == int:
seqnum_received = message
except Exception as e:
print(e)
def send():
rawmsg = message_entry.get()
if os.path.exists(rawmsg):
send_file(client_socket , rawmsg)
else:
packets = packeting(rawmsg)
for packet in packets:
while True:
x = str(packet)
serverSocket1.sendto(x.encode(), ("127.0.0.1", 12000))
try:
if seqnum_received == packet[1] + 1:
break
except Exception as e:
print(e)
serverSocket1.sendto(str(True).encode(), ("127.0.0.1", 12000))
chatbox.insert(tk.END , "n|You: " + rawmsg)
message_entry.delete(0, tk.END)
When testing on a noisy channel, the messages were not getting through.
New contributor
eb21 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.