|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
||||
|
||||
|
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
|
||||
|
||||
|
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/Py...ets/node10.html Hope this helps! ![]() |
|
#3
|
||||
|
||||
|
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! |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Network sockets problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|