#1
  1. Wacky hack
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Location
    London, England
    Posts
    513
    Rep Power
    14

    Network sockets problem


    Hullo,

    I'm writing a network game, and I've hit a little snag that is caused more by my lack of clarity about the use of sockets in Python than any syntax error. Basically, the game's logic works from within the function main(), which looks something like this:

    Code:
    def main():
    	set-up game stuff
    	if server:
    		sockobj = socket(AF_INET, SOCK_STREAM)
    		sockobj.bind((hostname,port))
    		sockobj.listen(1)
    		connection, address = sockobj.accept()
    	more set-up stuff
    
    
    	while 1:
    		for event in pygame.event.get():
    			monitor for keyboard events
    		if server:
    			if connection.recv(1024):
    				data = connection.recv(1024)
    				if not data: break
    				print data
    
    		update screen
    Usually, without the server bits, it all works fine. Now it seems to start up the game code, and get into the loop. It recognises when the client connects, and prints to the shell any data the client sends, but it won't respond to any keyboard events.

    The sockets code seems to be "hogging" the input. Is this what is happening? Can somebody give me a hint here as to how I should go about this?
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Well, for one thing, the accept() call blocks until it receives a connection. What you're looking for is a non-blocking socket IMHO.
    Try http://aspn.activestate.com/ASPN/Pyt...ts/node10.html

    Hope this helps!
  4. #3
  5. Wacky hack
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Location
    London, England
    Posts
    513
    Rep Power
    14
    Aha, this looks promising:

    socket.setblocking(0)

    It stops the recv() function from waiting for some data before returning, so it should just allow the loop to procede, and only act on data if it gets it. I'll wander back to my room and give it a go; thanks!

IMN logo majestic logo threadwatch logo seochat tools logo