【发布时间】:2023-04-07 21:55:01
【问题描述】:
我刚刚发布了我的程序的一部分。第一次运行程序时,我可以提供来自客户端的输入,服务器越过接受,但是当程序运行第二个循环时,它会卡在mysocket.accept 中。使套接字非阻塞并不能解决此问题。有谁知道如何清除此错误?
class Memory(threading.Thread):
def __init__ (self):
threading.Thread.__init__ (self)
def run(self):
global data_queue
while True:
sleep(0.1)
mysock.listen(5)
print "waiting for data"
conn, addr = mysock.accept()
print "received data from client"
data = conn.recv(1000)
data_queue.put(data)
class Execute(threading.Thread):
def __init__ (self):
threading.Thread.__init__ (self)
def run(self):
global data_queue
while True:
if not data_queue.empty():
data = data_queue.get()
if not data:
break
if data == b'on':
print "on"
gpio.output(4,True)
if data == b'off':
print "off"
gpio.output(4,False)
客户程序:
try:
a = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
print("Failed to create socket")
sys.exit()
a.connect(('127.0.0.1', 1236))
while True:
print "1.ON 2.OFF"
choice = input('Enter your choice')
if choice == 1:
try:
a.sendall(b"on")
except socket.error:
print("Failed to send")
sys.exit()
if choice == 2:
try:
a.sendall(b"off")
except socket.error:
print("Failed to send")
sys.exit()
ms.close()
【问题讨论】:
-
你知道socket.accept()在做什么吗?
-
是的,我知道socket.accept的基本功能
标签: python class sockets raspberry-pi serversocket