By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,171 Members | 809 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,171 IT Pros & Developers. It's quick & easy.

mulithreaded server

P: n/a
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_S TREAM)
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 ???
Mar 11 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
asit <li*****@gmail.comwrote:
>
In the above program, why there is an unhandeled exception ???
Probably because the code as you posted it has at least a half-dozen
mistakes.
>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()
You do not need the "global" statement there, since you are not changing
either stdoutlock or cSocketlock. However, you ARE setting "p", so you
need a "global p". Otherwise, you are simply creating a variable called
"p" that is local to the function, and which disappears when the function
returns.

Calling a global variable "p" is a very bad practice, by the way.
>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()
Same comments. You do not need the "global" statement you have, but you do
need "global p" if you want to change the global version of "p". Further,
as Jean-Paul pointed out and you rather rudely ignored, there is no
variable called "sSocketlock", because you commented it out.

Next, "stdout.release()" will fail. "stdout" does not have a release
function. You meant "stdoutlock.release()".

Next, you release sSocketlock, but you never acquired it. And if data does
not equal "q", you repeatedly acquire stdoutlock, but you never release it.

Fix these problems, and then see if you can ask for help in a more thorough
fashion.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Mar 13 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.