Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 28th, 2003, 06:49 PM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 513 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 38 m 37 sec
Reputation Power: 13
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?

Reply With Quote
  #2  
Old January 29th, 2003, 12:16 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 9 h 47 m 28 sec
Reputation Power: 4080
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!

Reply With Quote
  #3  
Old January 29th, 2003, 05:34 AM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 513 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 38 m 37 sec
Reputation Power: 13
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Network sockets problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap