Asynchronous Socket with the ServerSocket module (??)

James T. Dennis jadestar at idiom.com
Mon Apr 29 23:12:57 EDT 2002


guyon moree <cheezus at wanadoo.nl> wrote:

> hi all,

> i'm trying to write some sort of server program on linux. i stumbled into
> the ServerSocket module and it does pretty much what i want. i create a
> requesthandler class and pass it to the module and call server_forever() By
> using ForkingTCPServer i am able to make it handle multiple connecties.

> all fine...

> but...

> how can i keep a list of connections and pass for example messages from one
> client to the other through the server?

> thanx in advance, i am a newbie though.

> regards,
> Guyon

	Sorry I don't have time to dig up the modules and write a
	working sample, but I suspect that you'd do something like:

	* create your own class (to hold the list of active connections)
	and a couple of methods to add and remove to/from it (accessors)
	(alternatively just make a global list or dictionary --- possibly
	of tuples --- or of socket descriptors, at least)

	* over-ride the accept/handle_connection methods in your socket
	server to add new connections to your list/dictionary/class instance

	* over-ride the close/teardown method in your socket server to
	remove closed connections.

	* finally modify the method (which you surely most have already 
	over-ridden) to implement your connection lookup and message
	passing in the receiver handler of your connects.  (In other words
	when you receive data from one of the connections you look up
	where it's going in your dictionary/list/instance and then 
	try to perform the relay.  

	Naturally there's lots of fussy bits that you have to handle
	in there.  First, you have to find the actual method (names and
	prototypes) that have to be over-ridden.  Also you have to figure
	out how you want to handle exceptions and what you blocking 
	semantics should be (you'd like to avoid deadlock so you probably
	need to use non-blocking reads or non-blocking writes, pick one
	and be consistent).  Most importantly you have to figure out what
	sorts of exception handling are appropriate to your needs.

	I'd consider whipping up something (it would be a good learning
	exercise for me) but my wife will be very unhappy if I don't get
	her out of the house and off to dinner soon!

domestic-bliss-over-rules-hacking-ly yrs?





More information about the Python-list mailing list