Archer Hu
Archer Hu

Reputation: 37

How can I stop udp server in threading?

I wrote a udp no echo server in my program, I used thread to running and listen some message send by others. But it seems I couldn't stop it use tl.stop(), when I input q or quit. Some of my code is following:

            class treadListen(threading.Thread):
                def __init__(self):
                    self.running = True
                    threading.Thread.__init__(self)

                def run(self):
                    address = ('localhost', 16666)
                    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                    sock.bind(address)
                    while True:
                        data = sock.recv(65535)
                        print "MESSAGE:{0}".format(data)
                    sock.close()

                def stop(self):
                    self.running = False
                # end of class thread_clock


            if __name__ == "__main__":
                tl = treadListen()
                tl.start()

                while True:
                    message = raw_input("CMD>")
                    if not message:
                        print "Please input command!"
                        continue
                    elif (message == 'quit') or (message == 'q'):
                        tl.stop()
                        break
                    else:
                        print "input is {0}".format(message)
                        # do something
                        continue
                print "[CONNECTION CLOSED!]"

I was trying to add sock.shutdown(socket.SHUT_RDWR) and sock.close() to def stop of class, but it doesn't work. How can I stop the thread safety? Thanks!

Upvotes: 0

Views: 1414

Answers (2)

Archer Hu
Archer Hu

Reputation: 37

Thanks ntki,rbp,st. The problem solved use following code:

            class treadListen(threading.Thread):
                def __init__(self):
                    **self.running = True**
                    threading.Thread.__init__(self)

                def run(self):
                    address = ('localhost', 16666)
                    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                    **sock.settimeout(1)**
                    sock.bind(address)
                    **while self.running:
                        try:
                            data = sock.recv(65535)
                            print "MESSAGE:{0}".format(data)
                        except Exception, e:
                            continue**
                    sock.close()

                def stop(self):
                    **self.running = False**
                # end of class thread_clock

Upvotes: 0

st.
st.

Reputation: 589

Your while loop while True: works forever, so I guess your close or shutdown calls to the socket never can gets in way to work.

You should change while True: to while self.running: and that should do the trick.

Upvotes: 1

Related Questions