#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    34
    Rep Power
    10

    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:

    PHP Code:
    sock socket.socketsocket.AF_INET socket.SOCK_STREAM )

    sock.bind( ( 'localhost' 5376 ) )

    sock.listen)

    print 
    "Server initiated; entering acceptance loop..."

    while 1:
        
    conn sock.accept()

        
    temphndl sockwatchconn recvd )

        
    slaves.appendtemphndl )

        print 
    "[" time.strftime("%X") + "] Accepted client" 
    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:

    PHP Code:
    class sockwatchthreading.Thread ):
        
    def __init__self sockethndl queuehndl ):
            
    self.socket sockethndl
            self
    .queue queuehndl
            
    print "Sockwatch instance created..."
            
    threading.Thread.__init__self )
        
    def runself ):
            
    breaksess 0
            
    while not breaksess:
                
    data self.recvcmdself.socket )
                if 
    not data == 0:
                    
    self.queue.putdata )
                    print 
    "Put data into queue..."
                
    else:
                    
    breaksess 1
        def recvcmd
    sockethndl ):
            
    breakloop 0
            result 
    ''
            
    while not breakloop:
                
    byte sockethndl.recv)
                if 
    byte == "=":
                    
    breakloop 1
                
    else:
                    
    result += byte
            
    return result 
    does anyone know why this is happening?

    Beeyah
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    The first thing that jumps out is that socket.accept() returns a tuple, not a socket object:

    Accept a connection. The socket must be bound to an address and listening for connections. The return value is a pair (conn, address) where conn is a new socket object usable to send and receive data on the connection, and address is the address bound to the socket on the other end of the connection.
    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.

    Have fun,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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) .
    programming language development: www.netytan.com Hula

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    34
    Rep Power
    10
    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?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    34
    Rep Power
    10
    nevermind :D I realize now I didn't start() my thread :P

IMN logo majestic logo threadwatch logo seochat tools logo