Detecting a closed socket?

BlitzMax Forums/BlitzMax Programming/Detecting a closed socket?

Method Listen()

        Const MAXCONNECTIONS = 1000
	global read_set:Int[MAXCONNECTIONS]
	Local count:Int = 0                 'the total number of current connections
	Local num_active:Int                'the number that have activity this frame
	Local readptr:Int Ptr               'will end up pointing to the read_set[]
	
	'--------------------Build the ReadSet--------------------------------------
	For Local conn:TConnection = EachIn connection_list    'connection_list is a global list of all connections
		
		read_set[count] = conn._socket
		readptr = read_set 
		count:+ 1						'get an accurate count of total conn's
			
	Next
	'---------------------------------------------------------------------------	
	'------------------Call select, get number of active------------------------
	'---------------------------------------------------------------------------
	num_active = select_( count , readptr,0,Null,0,Null,timeout )
	
	'------------if we've got any activity, we've got to act on it--------------
	If num_active <= 0
		Return False
	Else
		?debug
		Print "Num active sockets: " + num_active
		Print "Total count: " + count
		?
		
		Local activity_list:Int[] = New Int[num_active]
		Local activity_count:Int  = 0
		
		
		'------------------This is a hack to help emulate FD_ISSET-------------
		'------------------this builds the activity list ----------------------
		'-------------------a list of connections with activity----------------	
		'----------------------------------------------------------------------	
		For Local iter = 0 Until count	
			
			If read_set[iter] <> 0
				activity_list[activity_count] = read_set[iter]
				activity_count:+ 1
				num_active:- 1
				
				If num_active = 0
					?debug
					Print "Total # iterations: " + (iter + 1)
					?
					Continue	'break out of the loop if we've counted all active connections
				EndIf
			EndIf
		Next
		
		'-----------------------------------------------------------------------
		'-----------Test each connection against the activity list--------------
		'-----------------------------------------------------------------------	
		For Local conn2:TConnection = EachIn connection_list
			For Local iter2 = 0 Until activity_count
				If conn2._socket = activity_list[iter2]
					?debug
					Print "Calling receive!"
					?
					ConnectionReceive( conn2 )    'Replace with your own TSocket.Recv() call.
				EndIf
			Next
		Next
		'-----------------------------------------------------------------------
	EndIf

EndMethod


For all intents and purposes, the TConnection in the code is a TSocket (it actually extends TSocket).

I don't have the time right now to fully explain the code, although if you know how Select() based polling works it might, hopefully be at least semi-understandable. It is ugly, and fairly poorly commented. I apologize for that.