August 4th, 2004, 04:41 AM
written base code for a server -- encountered errors; need help
Okay, so I'm working with two other people to make this service, and my job is making the server. However I'm having problems: first of all, for some reason it can't handle two clients, and I need someone to show me what I'm doing wrong. Actually, we'll just keep it at the for now. So let me show you my main acceptance loop:
So basically, it accepts the connection and sends a handle down to my "sockwatch" thread, which waits for input from the client. However, when I connect to my server again, the first connection is killed. The idea is it supports countless connections, but anyway, moving on, here is my sockwatch thread:
sock = socket.socket( socket.AF_INET , socket.SOCK_STREAM )
sock.bind( ( 'localhost' , 5376 ) )
sock.listen( 5 )
print "Server initiated; entering acceptance loop..."
conn = sock.accept()
temphndl = sockwatch( conn , recvd )
slaves.append( temphndl )
print "[" + time.strftime("%X") + "] Accepted client"
does anyone know why this is happening?
class sockwatch( threading.Thread ):
def __init__( self , sockethndl , queuehndl ):
self.socket = sockethndl
self.queue = queuehndl
print "Sockwatch instance created..."
threading.Thread.__init__( self )
def run( self ):
breaksess = 0
while not breaksess:
data = self.recvcmd( self.socket )
if not data == 0:
self.queue.put( data )
print "Put data into queue..."
breaksess = 1
def recvcmd( sockethndl ):
breakloop = 0
result = ''
while not breakloop:
byte = sockethndl.recv( 1 )
if byte == "=":
breakloop = 1
result += byte
August 4th, 2004, 06:52 AM
The first thing that jumps out is that socket.accept() returns a tuple, not a socket object:
So self.socket in sockwatch() will be a tuple. The reason I mention this is because you try and call the recv() method on this tuple in recvcmd(), which should cause an error.
You might also want to make the while loop simpler by doing while True and using break when you need to exit.
August 4th, 2004, 07:07 AM
You might also find the asyncore module insteresting, since its was made for the kind of thing you're doing (or seem to be doing) .
August 4th, 2004, 01:36 PM
Thank you very much for your response However, I had still been having problems with receiving data even after making the changes you suggested, so i decided to make it print a harmless message as soon as sockwatch entered the run() method. the problem is, the message is never printed. is there anything in my code that would prevent run() from executing?
August 4th, 2004, 01:38 PM
nevermind :D I realize now I didn't start() my thread :P