Closed Socket is still running!

BlitzMax Forums/BlitzMax Programming/Closed Socket is still running!

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Below is some test code for a small multi-user program I am developing; but I have a problem... If the server closes with clients attached; I do not seem to be able to detect it until I exit the application.

Run this twice on your local system (Load Blitzmax twice), and click on Server on one, and Client on the other.

You can start and stop (ESC) the client (When the server is running), and the server detects this, but stop the server and the client does not notice...

Am I closing the Socket correctly in the Server code?

multiuser.bmx
SuperStrict

Const GS_MODE% = 0
Const GS_CLIENT% = 1
Const GS_SERVER% = 2

Const SERVER_ADDRESS$ = "127.0.0.1"
Const SERVER_PORT% = 31245

Graphics 320 , 200
Global gw% = GraphicsWidth()
Global th% = TextHeight( "A" )
Global state% = GS_MODE
Global quit% = False

'#
Global Server:TSocket
Global Client:TSocket
Global Clients:Tlist = CreateList()

'############################################################
Repeat
	Delay(10)
	Cls
	Select state
	'--------------------
	Case GS_MODE
		SetColor( 255,255,0 )
		centerText( "CLIENT" , 0 , 50 , GW/2 )
		centerText( "SERVER" , GW/2 , 50 , GW/2)
		centerText( "Press ESC to Exit" , 0 , 150 , GW)
		DrawLine( gw/2,0,gw/2,GraphicsHeight() )
		If KeyHit( KEY_ESCAPE ) Then quit = True
		If MouseHit(1) Then
			If MouseX() < gw / 2 Then 
				client = createClient( SERVER_ADDRESS , SERVER_PORT )
				If client Then
					Print "#CLIENT CONNECTED"
					state = GS_CLIENT
				Else
					Print "** ERROR CONNECTING TO SERVER"
				End If
			Else
				server = createServer( SERVER_PORT )
				If server Then 
					Print "#SERVER STARTED"
					state = GS_SERVER
				Else
					Print "** ERROR STARTING SERVER"
				End If
			End If
		End If
	'--------------------
	Case GS_CLIENT
		centerText( "CLIENT" , 0 , TH , GW )
		centerText( "Press ESC to Exit" , 0 , TH*2 , GW )
		If KeyHit( KEY_ESCAPE ) Then 
			state = GS_MODE
			CloseSocket( client )
			Print "#CLIENT STOPPED"
		Else
			'# Check server still connected
'##### THIS DOES NOT APPEAR TO WORK #####
			If Not SocketConnected(client) Then
				Print "<Server connection lost"
				state = GS_MODE
				CloseSocket( client )
			End If
		End If
	'--------------------
	Case GS_SERVER
	Local conn:TSocket
		centerText( "SERVER" , 0 , TH , GW )
		centerText( "Press ESC to Exit" , 0 , TH*2 , GW )
		'# Are we closing the server?
		If KeyHit( KEY_ESCAPE ) Then 
			state = GS_MODE
			CloseSocket( server )
			Print "#SERVER STOPPED"
		Else
			'# Check for NEW connections
			conn = AcceptClient( server )
			If conn Then
				Print ">Client Connected from " + DottedIP(SocketRemoteIP(conn))
				clients.addlast( conn )
			End If
			'# Check connections
			For client = EachIn clients
				If Not SocketConnected(client) Then
					Print "<Client disconnected"
					clients.remove( client )
					CloseSocket( client )
				End If
			Next
		End If
	End Select
	Flip
Until quit

'############################################################
Function centerText( txt$, x%, y%, w% )
DrawText( txt$ , x+( w-TextWidth(txt$)) / 2 , y )
End Function

'############################################################
Function CreateServer:TSocket(port:Int)
	Local server:TSocket = New TSocket
	server = CreateTCPSocket()
	If Not BindSocket(server, port) Or Not SocketListen(server) Then
		CloseSocket(server)
		Return Null
	EndIf
	Return server
EndFunction
	
'############################################################
Function CreateClient:TSocket(address:String, port:Int)
	Local client:TSocket = New TSocket
	client = CreateTCPSocket()
	If Not ConnectSocket(client, HostIp(address), port)
		CloseSocket(client)
		Return Null
	EndIf
	Return client
EndFunction

'############################################################
Function AcceptClient:TSocket(server:TSocket)
Local accepted_socket:TSocket = SocketAccept(server)
Local client:TSocket 
	If Not accepted_socket Return Null
	client = New Tsocket
	client = accepted_socket
	Return client
EndFunction