diff --git a/demo.py b/demo.py index 49ebf5a..50bd62e 100644 --- a/demo.py +++ b/demo.py @@ -7,7 +7,7 @@ port = int(sys.argv[2]) if sys.argv[1] == "s": sock.listen(("0.0.0.0", port)) while True: - for (d, a) in sock.poll(): + for d, a in sock.poll(): print(d, a) time.sleep(0.1) elif sys.argv[1] == "c": diff --git a/network.py b/network.py index 7ae6164..7cbe3c0 100644 --- a/network.py +++ b/network.py @@ -9,6 +9,8 @@ class Sock: self.sock_thread = None def send(self, message, address): + if type(message) == str: + message = message.encode() self.sock.sendto(message, address) def listen(self, address, length=65535): @@ -16,7 +18,7 @@ class Sock: self.sock_thread.start() def poll(self): - if len(self.queue_lock) == 0: + if len(self.queue) == 0: return [] with self.queue_lock: queue = self.queue @@ -25,14 +27,13 @@ class Sock: class SockThread(Thread): def __init__(self, sock, address, length): - super.__init__(super) + Thread.__init__(self) self.sock = sock - self.sock.sock.bind(address) - self.length = self.length + self.length = length self.sock.sock.bind(address) def run(self): while True: - r = self.sock.sock.recv(self.length) + r = self.sock.sock.recvfrom(self.length) with self.sock.queue_lock: self.sock.queue.append(r)