程序卡在接受语句上 [英] Program getting stuck at accept statement

查看:65
本文介绍了程序卡在接受语句上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发布了程序的一部分.第一次运行该程序时,我可以从客户端提供输入,而服务器会越过接受,但是当该程序运行第二个循环时,它将卡在 mysocket.accept 中.使套接字无阻塞并不能解决这个问题,有人知道如何清除此错误吗?

I've just posted a part of my program. The first time I run the program I could give the input from a client and the server crosses the accept, but when the program runs for second loop it gets stuck in the mysocket.accept. Making the socket non-blocking doesn't solve this.Does anyone know how to clear this error?

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()

推荐答案

我相信您在内存线程中想要的是这样的:

I believe what you want in your Memory thread is this:

def __init__ (self):
    threading.Thread.__init__ (self)

def run(self):
    global data_queue
    mysock.listen(5)
    print "waiting for data"
    while True:
        sleep(0.1)
        conn, addr = mysock.accept()
        print "received connection from client"
        self.talk_to_client(conn)

def talk_to_client(self, conn):
    data = conn.recv(1000)
    while data != 'quit':
        reply = prepare_reply_to_client(data)
        data_queue.put(reply)
    conn.close()     # if we're done with this connection

请注意我如何将监听移至while循环上方,因此它仅发生一次.您的问题是您对listen()的第二次调用与第一次调用冲突.您只应拨打一次监听.随后的accept将克隆侦听套接字并将其用于连接.完成操作后,您可以关闭该连接,但是您的收听会继续等待新的连接.

Notice how I've moved the listen up above the while loop so it only happens once. Your problem is that your second call to listen() conflicts with the first. You should only call listen once. Subsequent accepts will clone the listen socket and use it for a connection. You then close that connection when your done with it, but your listen continues waiting for new connections.

这是python文档中的规范示例: https://docs.python.org/2/library/socket.html#example

Here's the canonical example in the python docs: https://docs.python.org/2/library/socket.html#example

更新:通过编写方法talk_to_client(conn)添加与客户端的扩展交互的示例

Updated: Add example of extended interaction with client by writing method talk_to_client(conn)

这篇关于程序卡在接受语句上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆