TStreamReadException?

BlitzMax Forums/BlitzMax Programming/TStreamReadException?

*(Posted 2008) [#1]
Is there any way to see how and where this is thrown im trying to look at it and the docs are about as helpful as a chocolate teapot.

Basically I get a null error from a stream when reading, I know full well this stream was created at creation time (I have one function to create it and another to destroy it). I have gone entirely through the code (all 10000~ lines) and there are no other closestream or closesocket references. I just want to add a check to make sure the stream exists.

Anyone have any ideas on finding out if a stream exists or not?


plash(Posted 2008) [#2]
stream.Eof()?


*(Posted 2008) [#3]
basically im reading from a Tsocketstream and the code line that throws the error is:
PacketData = PacketData + ReadBytes( ClientData.Stream, AvailableBytes-1 )


Now the socket exists because thats checked first, and when the stream is closed the socket is closed at the same time and they are set to null for checking. The problem is it still throws the error. Its as if the stream vanishes.


Dreamora(Posted 2008) [#4]
is AvailableBytes updated right in front of it as well?


*(Posted 2008) [#5]
yup, heres the small code segment

						'see if there is any data in the socket
						AvailableBytes = SocketReadAvail( ClientData.Socket )
						If AvailableBytes>19				'19 is the size of an empty packet
							PacketData = ""
							ExtractedPacket = ""
							Important = "F"
							PacketClass = 0
							NewPacket = ""
							PacketData = PacketData + ReadString( ClientData.Stream, AvailableBytes-1 )'Chr$( ReadByte( ClientData.Stream ) )



tonyg(Posted 2008) [#6]
What happens when you print the string read from clientdata.stream?
Did this code ever work and, if so, what has changed?
You mention there are 10000 lines so I am guessing you've compiled and run it a few times so what did you add since it worked?
How many packets are read before the error? Maybe keep some count of how many times it gets updated and a type to save each packet content which you output as trace data?


*(Posted 2008) [#7]
it throws an error at the PacketData = PacketData + ReadString... bit so I cannot print it out. It says its a null error but there is only one closestream command in the entire file and one closesocket command that is in the removeclient function and after that they are set to null before the entry is removed from the list.

I just wanted a way to find out if a stream is valid and if it exists.


tonyg(Posted 2008) [#8]
Oh OK. Good luck
<Edit> Socketconnected()?


*(Posted 2008) [#9]
SocketConnected is to test sockets, unfortunately it doesnt work on streams. Would love a streamconnected function tho :)


tonyg(Posted 2008) [#10]
Don't you need a socket to create a socketstream? Doesn't the socket get autoclosed when the stream closes is autoclose=true is set?
<edit> It's possible that the socketa nd/or socketstream have closed somehow but it might be best to test in case something else is happening.


*(Posted 2008) [#11]
yup that is correct, this is how I know the stream should have been closed I have a RemoveClient function that closes all streams and sockets for me, its the only function in the entire code file that does it (basically it makes it easier to keep track if I just call RemoveClient instead of closing sockets here there and everywhere).


tonyg(Posted 2008) [#12]
So you have good reasons to think the socketstream is still open except it is returning null.
So...
Did this code ever work and, if so, what has changed?
How many packets are received successfully?
If packets are received print/save them and check them against the input to the stream.


*(Posted 2008) [#13]
The socket is there as I call if SocketConnected( ClientData.Socket ) before going into checking the stream. The only time the stream is destroyed is if the socket is destroyed with it via RemoveClient. There is no other closestream/closesocket calls in the entire program.


tonyg(Posted 2008) [#14]
I understand that.
That means either
a) Something else has happened to the socket/stream to close it
or
b) The socket/stream is still open and something else is the problem.


*(Posted 2008) [#15]
this is why I wondered if there was a way of checking is a stream exists, and if so how?


tonyg(Posted 2008) [#16]
stream.eof()
From the source :
For communication type streams such as socket streams, Eof returns True if the stream
has been shut down for some reason - either locally or by the remote host. In this case,
no more bytes can be read from or written to the stream.


but this was mentioned earlier.


*(Posted 2008) [#17]
strangely enough it returns false here :s

Will look into it more tomorrow :s


tonyg(Posted 2008) [#18]
Which means it is likely to be still open.
Really, answer the following as it might help :
Did this code ever work and, if so, what has changed?
How many packets are received successfully?
If packets are received print/save them and check them against the input to the stream.


*(Posted 2008) [#19]
the code worked fine, somewhere along the line it has stopped working and for the life of me I havent a clue why as I havent adjusted anything relating to streams or sockets. Basically ive been working on position packets.


tonyg(Posted 2008) [#20]
So how many packets are received before the error?
If it always fails at a certain packet what *would* it have been?


*(Posted 2008) [#21]
Its random thats what makes it harder to find


tonyg(Posted 2008) [#22]
The TStreamReadException is thrown when the number of bytes to be read are not available normally due to the end of file.
Can you catch the exception and then do a readbyte loop until the error occurs again? If you check the bytes read and packets received it might give some idea where the problem is occurring.


Dreamora(Posted 2008) [#23]
funny, the code above uses readstring not readbytes. thought that normally should not make a difference it makes a massive difference if you send chr(0) or wrote a string into the packet on the other end.


*(Posted 2008) [#24]
tonyg how do you catch the exception ive tried to use the try ...catch...endtry but catch required a type and the docs dont explain it enough from that it looks like a exit command that was in blitz3d.

it did use readbyte but that threw an error and I was trying to see if anything was faster


tonyg(Posted 2008) [#25]
Try throws as string which catch takes as argument.
If the issue is seen with readbytes then what is the previous byte value?
I really would consider creating a trace_string type and using it to find what is successfully received and what causes the error.
Even if it is random there might be some pattern to the problem.


grable(Posted 2008) [#26]
Try throws as string which catch takes as argument.

A thrown exception can be any object, not just strings.
And you can mix and match types to trap the exception you want.
Type TTest
	Method ToString:String()
		Return "TTest"
	EndMethod
EndType

Try
	Throw "String"
	'Throw TRuntimeException.Create("exception")
	'Throw New TTest
Catch e:String
	Print e
Catch e:TRuntimeException
	Print e.ToString()
Catch e:Object
	Print e.ToString()
EndTry

Note the order of the catch statements, since a TBlitzException is also an Object it must come first.


*(Posted 2008) [#27]
thanks guys will have a look