Sockets and threading

Z

zayatzz

Im trying to get aquinted to python on bit more basic level and am
following socket and threading programming tutorials from these 2
addresses :

http://heather.cs.ucdavis.edu/~matloff/Python/PyNet.pdf
http://heather.cs.ucdavis.edu/~matloff/Python/PyThreads.pdf

in this PyThreads file he sets up threaded server app which listens to
what clients send to it and echos it back to clients while adding sent
info into one string.

The problem is that the code does not work for me.

class srvr(threading.Thread):
v = ""
vlock = threading.Lock()
id = 0
def __init__(self,clntsock):
threading.Thread.__init__(self)
self.myid = srvr.id
srvr.id += 1
self.myclntsock = clntsock
def run(self):
while 1:
k = self.myclntsock.recv(1)
if k == "": break
srvr.vlock.acquire()
srvr.v += k
srvr.vlock.release()
self.myclntsock.send(srvr.v)
self.myclntsock.close()

Instead of sendint back i get this error :
File "server3.py", line 31, in serveclient
k = self.myclntsock.recv(1)
File "/usr/lib/python2.6/socket.py", line 165, in _dummy
raise error(EBADF, 'Bad file descriptor')
socket.error: [Errno 9] Bad file descriptor

As much as i understand this line 31 is supposed to recieve stuff from
clients and has buffer size 1. I dont understand what this has to do
with file descriptor.. whatever that is anyway.

Alan
 
G

Gabriel Genellina

while 1:
k = self.myclntsock.recv(1)
if k == "": break
srvr.vlock.acquire()
srvr.v += k
srvr.vlock.release()
self.myclntsock.send(srvr.v)
self.myclntsock.close()

Instead of sendint back i get this error :
File "server3.py", line 31, in serveclient
k = self.myclntsock.recv(1)
File "/usr/lib/python2.6/socket.py", line 165, in _dummy
raise error(EBADF, 'Bad file descriptor')
socket.error: [Errno 9] Bad file descriptor

As much as i understand this line 31 is supposed to recieve stuff from
clients and has buffer size 1. I dont understand what this has to do
with file descriptor.. whatever that is anyway.

That means the clntsock variable isn't an open, available socket. Note
that you close the socket right after sending the response - are you sure
you don't have an indentation error there?
 
P

Piet van Oostrum

zayatzz said:
z> Im trying to get aquinted to python on bit more basic level and am
z> following socket and threading programming tutorials from these 2
z> addresses :
z> in this PyThreads file he sets up threaded server app which listens to
z> what clients send to it and echos it back to clients while adding sent
z> info into one string.
z> The problem is that the code does not work for me.
z> class srvr(threading.Thread):
z> v = ""
z> vlock = threading.Lock()
z> id = 0
z> def __init__(self,clntsock):
z> threading.Thread.__init__(self)
z> self.myid = srvr.id
z> srvr.id += 1
z> self.myclntsock = clntsock
z> def run(self):
z> while 1:
z> k = self.myclntsock.recv(1)
z> if k == "": break
z> srvr.vlock.acquire()
z> srvr.v += k
z> srvr.vlock.release()
z> self.myclntsock.send(srvr.v)
z> self.myclntsock.close()


The last line is wrongly indented. It should be outside the loop. Shift
it left so that it lines up with the while.

What happens now is that the socket is closed after the first recv().
Then the next round in the loop fails because the myclntsock is closed.
 
D

Dennis Lee Bieber

As much as i understand this line 31 is supposed to recieve stuff from
clients and has buffer size 1. I dont understand what this has to do
with file descriptor.. whatever that is anyway.
You didn't supply the important part... YOUR code where you create
an instance of the server -- that is where you supply the socket to be
used by it...
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top