Hi,
  I got a problem. I use a 'select' in a loop in the main thread, and
when select return, a new thread will be created to handle the network
event. And if the client send some special string, the thread will
change a global flag to false, so the loop in the main thread will
break. But it never work.

Below is the code:
--------------------- s.py (the server) --------------------------
import socket
import select
import thread
import sys

go_on = True

def read_send(s):
        s.setblocking(1)
        str = s.recv(1024)
        print 'recv:', str
        s.send(str)
        s.close()
        if (str == 'quit'):
                go_on = False
                print 'User quit...with go_on =', go_on
        return


s = socket.socket(socket.AF_INET)
s.bind(('', 9999))
s.listen(5)
s.setblocking(0)

while go_on:
        r_set = [s.fileno(),]
        r, w, e = select.select(r_set,[],[],0.5)
        print 'select returned with go_on =', go_on
        for rs in r:
                if rs == s.fileno():
                        ns, addr = s.accept()
                        print 'socket on', addr, 'has been accepted'
                        thread.start_new_thread(read_send, (ns,))
s.close()


--------------------- c.py (the client) --------------------------
import socket
import sys

if len(sys.argv) != 3:
        print 'usage: python c.py ip port'
        sys.exit(-1)

ip = sys.argv[1]
port = int(sys.argv[2])
s = socket.socket(socket.AF_INET)
s.settimeout(5)
s.connect((ip, port))
str = raw_input('please input:')
s.send(str)
str = s.recv(1024)
print 'received:', str
s.close()

--------------------------------------------------------------------------
run s.py first, and then run c.py as:
python c.py 127.0.0.1 9999
and then input 'quit', but the server never end :(



Thank you for your help!

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to