make it work
This commit is contained in:
parent
ccd9753276
commit
24a4126a8d
2 changed files with 7 additions and 6 deletions
2
demo.py
2
demo.py
|
@ -7,7 +7,7 @@ port = int(sys.argv[2])
|
||||||
if sys.argv[1] == "s":
|
if sys.argv[1] == "s":
|
||||||
sock.listen(("0.0.0.0", port))
|
sock.listen(("0.0.0.0", port))
|
||||||
while True:
|
while True:
|
||||||
for (d, a) in sock.poll():
|
for d, a in sock.poll():
|
||||||
print(d, a)
|
print(d, a)
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
elif sys.argv[1] == "c":
|
elif sys.argv[1] == "c":
|
||||||
|
|
11
network.py
11
network.py
|
@ -9,6 +9,8 @@ class Sock:
|
||||||
self.sock_thread = None
|
self.sock_thread = None
|
||||||
|
|
||||||
def send(self, message, address):
|
def send(self, message, address):
|
||||||
|
if type(message) == str:
|
||||||
|
message = message.encode()
|
||||||
self.sock.sendto(message, address)
|
self.sock.sendto(message, address)
|
||||||
|
|
||||||
def listen(self, address, length=65535):
|
def listen(self, address, length=65535):
|
||||||
|
@ -16,7 +18,7 @@ class Sock:
|
||||||
self.sock_thread.start()
|
self.sock_thread.start()
|
||||||
|
|
||||||
def poll(self):
|
def poll(self):
|
||||||
if len(self.queue_lock) == 0:
|
if len(self.queue) == 0:
|
||||||
return []
|
return []
|
||||||
with self.queue_lock:
|
with self.queue_lock:
|
||||||
queue = self.queue
|
queue = self.queue
|
||||||
|
@ -25,14 +27,13 @@ class Sock:
|
||||||
|
|
||||||
class SockThread(Thread):
|
class SockThread(Thread):
|
||||||
def __init__(self, sock, address, length):
|
def __init__(self, sock, address, length):
|
||||||
super.__init__(super)
|
Thread.__init__(self)
|
||||||
self.sock = sock
|
self.sock = sock
|
||||||
self.sock.sock.bind(address)
|
self.length = length
|
||||||
self.length = self.length
|
|
||||||
self.sock.sock.bind(address)
|
self.sock.sock.bind(address)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while True:
|
while True:
|
||||||
r = self.sock.sock.recv(self.length)
|
r = self.sock.sock.recvfrom(self.length)
|
||||||
with self.sock.queue_lock:
|
with self.sock.queue_lock:
|
||||||
self.sock.queue.append(r)
|
self.sock.queue.append(r)
|
||||||
|
|
Loading…
Reference in a new issue