Determining existence of stream?

Blitz3D Forums/Blitz3D Programming/Determining existence of stream?

Dip(Posted 2010) [#1]
Is there a command to determine if a variable is a valid stream (tcp in my case)? At some point in my program, my stream becomes not-a-stream, and when I go to end the app I get a mem access violation while trying to close connections. I tried to use "If Eof(stream) <> -1 Then" but it then errored on Eof saying stream was not a stream. Obviously it knows somehow, how can I access that info? I'm not seeing anything relevant in the command reference.


Warner(Posted 2010) [#2]
Did the handle variable become zero? Best is, when freeing/closing something to reset the handle to zero.
Ie:
file = readfile("test.txt")
closefile file: file = 0

After this, you could determine if the filestream is still valid by checking if (file<>0).


Dip(Posted 2010) [#3]
Apparently it's being closed because I am calling closetcpserver right before in the list of things to do before ending the app. I thought that once the connection was handed off with accepttcpstream, they were independent. Good to know. Still, I'd like to know if there's a way to check validity of a stream.


PowerPC603(Posted 2010) [#4]
I've posted a complete example in the code-archives of a server-client system, which includes the determination if a stream has been closed or not (client disconnected for example):

http://blitzbasic.com/codearcs/codearcs.php?code=2653

From the docs:

Eof returns 1 if eof has been reached or, in the case of a TCP stream, the stream has been 'nicely' closed.




Dip(Posted 2010) [#5]
Nice example, but I'm already checking for Eof and handling those appropriately. Eof itself errored out saying 'invalid stream'. I figured from the docs that
Eof returns -1 if something has gone wrong during stream processing.


would cover the variable being an invalid stream, but it does not.


NewtSoup(Posted 2010) [#6]
I haven't checked to make sure but doesn't the variable used to hold the stream reference become a positive integer if the stream is open and 0 if it's closed?

If so then:

If (myStreamVariable) Then
    
    ;do stuff here

Else
   
    ;do error message
End If


Should provide the error trapping you need.