Preventing crash on close stream
BlitzMax Forums/BlitzMax Programming/Preventing crash on close stream
| ||
I'm doing a webserver type app where people can download stuff using their webbrowser. The problem is that the app crashes, if the downloader closes their connection. It crashes in the following line: clientStream.writeBytes(bytes , read) As the clientStream is suddenly closed. Wrapping it in try catch does nothing. The app simply crashes as it attempts to write to the invalid stream. No info in debug mode. It just ends. |
| ||
Can you not detect the stream closing with Eof(clientStream) ? EG: If Not(clientStream.Eof()) clientStream.writeBytes(bytes , read) Else clientStream.Close() GoDoSomethingElse() End If |
| ||
No go. I can't prevent it from crashing. The app icon in the taskbar disappears but the app keeps running. (It's a console app) I can quit it using Escape from inside the IDE console. Just like before the app crashes. |
| ||
Isn't there a command to check if the stream is still alive? (don't know, perhaps even a =null check on the stream itself?) If not, using BNetEx and readavail (like in old blitz) might be a solution so you see when the stream breaks. Another not tested idea as this is an io thing: enclose it within try - catch. IO stuff etc throws errors. |
| ||
think there is an isConnected() or isSocketConnected() method somewhere in the socket docs. |
| ||
Is this on windows? My socket streams on windows work fine. I use Eof() to determine whether the client/server disconnected. No problems so far. |
| ||
It's on Mac. I'll test it on Windows tomorrow. |
| ||
Try this test server/client (compile both without gui app - these are meant for console). Press ctrl-c to disconnect on windows (end program) Server:- SuperStrict Global serverSocket:TSocket = CreateTCPSocket() serverSocket.Bind( 24010 ) serverSocket.Listen(0) Global serverStream:TStream = CreateSocketStream( serverSocket ) Global clientList:TList = New TList Function ServiceClients() For Local client:TSocketStream = EachIn clientList If Eof(client ) Print "Client " + client.ToString() + " disconnected" client.close() clientList.Remove( client ) client = Null Else If SocketReadAvail( client.Socket() ) Local line:String = client.ReadLine() If line Print "Msg from client " + client.ToString() + " :" + line client.WriteLine( line ) EndIf EndIf EndIf Next EndFunction Print "Server running. Waiting for connections." Repeat Local socket:TSocket = SocketAccept( serverSocket, 10 ) If socket Local stream:TSocketStream = CreateSocketStream(socket) clientList.AddLast( stream ) Print "Client " + stream.ToString() + " connected" EndIf ServiceClients() Forever Client:- SuperStrict Global stream:TStream Print "Attempting to connect to server..." Repeat If Not stream stream = TSocketStream.CreateClient( "localhost", 24010 ) If stream Print "Connected to server" EndIf Else If Eof( stream ) Print "Server disconnected. Reconnecting..." stream.Close() stream = Null Else Local s:String = Input( "Msg to server :" ) stream.WriteLine(s) Local line:String = stream.ReadLine() If line Print "Response from server :"+line EndIf EndIf EndIf Forever |