mulithreaded server

asit lipun4u at gmail.com
Tue Mar 11 11:24:54 EDT 2008


import socket
import sys
import thread

p=1
PORT=11000
BUFSIZE=1024

def getData(cSocket):
    global stdoutlock,cSocketlock
    while True:
        cSocketlock.acquire()
        data=cSocket.recv(BUFSIZE)
        if data=='q':
            data='client exited'
            cSocket.close()
            p=0
            cSocketlock.release()
        stdoutlock.acquire()
        stdout.write(data)
        stdoutlock.release()

def sendData(cSocket):
    global stdoutlock,cSocketlock
    while True:
        stdoutlock.acquire()
        data=raw_input('>>')
        cSocketlock.acquire_lock()
        if data=='q':
            stdout.write('server exited')
            stdout.release()
            p=0
            cSocket.close()
        sSocket.send(data)
        sSocketlock.release()


stdout=sys.stdout
host=''
sSocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sSocket.bind((host,PORT,))
sSocket.listen(1)
#sSocketlock=thread.allocate_lock()
stdoutlock=thread.allocate_lock()
print 'waiting for connection'
cSocket,addr=sSocket.accept()
print 'connection from',addr
cSocketlock=thread.allocate_lock()
thread.start_new_thread(sendData,(cSocket,))
thread.start_new_thread(getData,(cSocket,))
if p==0:
    sSocket.close()



In the above program, why there is an unhandeled exception ???



More information about the Python-list mailing list