Reliable UDP implementation using sequence numbers, deadlocking

566 views Asked by At

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.

0

There are 0 answers