import socket

class UDPReceiver:
    def __init__(self, ip_address, port):
        self.UDP_IP = ip_address
        self.UDP_PORT = port
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
        self.sock.bind((self.UDP_IP, self.UDP_PORT))
        print(f"Listening on {self.UDP_IP}:{self.UDP_PORT}")

    def receive(self):
        data, addr = self.sock.recvfrom(1024) # buffer size is 1024 bytes
        print(f"Received message from {addr}")
        return data

    def close(self):
        self.sock.close()