0

I am trying to implement a reliable UDP messaging scheme between a single client and a server.

In my current code I can send incrementing numbers by 1 to the server if he only uses the receive command. if the server tries replying with the received data using the send command, it seems to work for 1-3 messages back and forth, and then I enter a deadlock. I do not understand where the source of the deadlock comes from. Below is my implementation of send and receive.

Both the client and server start with their self.seqnumber set to 0 and both sockets are set to timeout after 1 second. Both the client and server share these methods as they belong to a class both the client and server import.

def sendCommand(self, command):
    self.s.sendto((self.seqnumber).to_bytes(8, "little") + command, self.address)
    try:
        data, self.address = self.s.recvfrom(1200)
        if int.from_bytes(data[:8], 'little') == self.seqnumber:
            self.seqnumber += 1
            return 0
        else:
            return self.sendCommand(command)
    except:
        return self.sendCommand(command)

def getCommand(self):
    while(1):
        try:
            data, self.address = self.s.recvfrom(1200)
            if int.from_bytes(data[:8], 'little') == self.seqnumber:
                self.s.sendto(data, self.address)
                self.seqnumber += 1
                break
            elif int.from_bytes(data[:8], 'little') < self.seqnumber:
                self.s.sendto(data, self.address)
            else:
                continue
        except:
            continue
    return data[8:]

The code running on the server (commInf is the class in which the get and send command are defined):

while (1):
        command = self.commInf.getCommand()
        print(command.decode())
        self.commInf.sendCommand(command)

and the code running on the client:

for i in range(100):
    self.commInf.sendCommand(f"{i}".encode())
    command = self.commInf.getCommand()
    print(command.decode())

I expect the output to allow me to reliably send messages and return them using the sendCommand(with the received data) since the returned data from getCommand does not include its sequence number and is just the raw data.

Lcas
  • 11
  • 3
  • 1
    What's the point of this? Isn't this what you get for free by using TCP? – Barmar Nov 05 '19 at 00:47
  • This was for a school assignment and I am now doing it out of personal interest, trying to understand my mistakes. – Lcas Nov 05 '19 at 00:53
  • Try changing the `else:` and `except:` blocks to print something. Calling the same function when you get an error seems wrong. – Barmar Nov 05 '19 at 00:53
  • And use `tcpdump` or `Wireshark` to monitor the packets. – Barmar Nov 05 '19 at 00:54
  • after a few print statements it seems both client and server deadlock trying to receive a send acknowledgement. Is there a simpler way to code a reliable UDP send and receive function? Thank you. – Lcas Nov 05 '19 at 01:01
  • Take a look at the TFTP protocol. https://tools.ietf.org/html/rfc1350 – Barmar Nov 05 '19 at 01:06

0 Answers0