[cse491] hw3 help

C. Titus Brown ctb at msu.edu
Mon Sep 15 18:13:06 PDT 2008


On Mon, Sep 15, 2008 at 06:51:23PM -0400, Dinesh Banda wrote:
-> I need some help on homework3. I don't understand the non-blocking concept. 
-> I looked at the notes but I'm completely lost...what does "doesn't block" 
-> and "aborts and signals an error" mean (from lecture 3 outline)? 

This is why you should ask questions in lecture and lab :)

A blocking call is one that stops execution of the code ("blocks") while
waiting for an event.  For example, in your HW #2,

	(client_sock, stuff) = sock.accept()

stops the program until a network connection is made into 'sock', at
which point 'sock' returns.

A non-blocking call is, of course, one that doesn't block. By 'aborts
and signals an error' I mean that an exception is raised; for example,

	sock.setblocking(0)
	(client_sock, stuff) = sock.accept()

will raise a socket.error exception that you have to catch, e.g.

	try:
		client_sock, stuff = sock.accept()
	except socket.error:
		print 'no connection waiting'

The goal of the homework is to set *both* the bound socket and the
received sockets to non-blocking, and then monitor them for activity
appropriately (i.e. check to see if connections are waiting on the bound
socket with 'accept', and check to see if data needs to be read (and
written) on the existing connections).  Unlike the blocking echo server,
which could only process data from a single connection at a time, this
server will be able to process data from multiple connections.

-> I've looked at one of your emails and tried to follow instructions but I 
-> don't know how to accept multiple connections and put them in a list. It 
-> would be great if you show an example! 

socket objects are just another object to Python, so you can put them in
any ol' list; for example, 

	try:
		client_sock, stuff = sock.accept()

		# this only runs if 'sock.accept' doesn't raise an
		# exception
		connections.append(client_sock)

	except socket.error:
		# this only runs if 'sock.accept' *does* raise an
		# exception
		pass		# do nothing

--titus
-- 
C. Titus Brown, ctb at msu.edu



More information about the cse491-fall-2008 mailing list