TWinSocketStream read of empty socket


2004-04-30 03:37:33 AM
cppbuilder76
Hello
I have been fighting with a bug the past couple of days and finally narrowed
it down to a
call to the Read() method of a TWinSocketStream when there is nothing being
sent
from the server.
Original Code:
TWinSocketStream *Stream;
char Buffer[5121];
AnsiString Command;
Stream = // GET STREAM FROM ANOTHER CLASS THAT KEEPS TRACK OF THEM...
...
Command = "Some Server Command";
Stream->Write (Command.c_str (), Command.Length ());
memset (Buffer, 0, sizeof (Buffer));
if (Stream->Read (Buffer, sizeof (Buffer) - 1) <= 0) {
// ERROR STUFF
}
// PROCESS INITIAL DATA RECEIVED
memset (Buffer, 0, sizeof (Buffer));
while (Stream->Read (Buffer, sizeof (Buffer) - 1)>0) {
// PROCESS NEXT
memset (Buffer, 0, sizeof (Buffer));
}
...
This would trigger a problem somewhere in the memory of the stream so that
next time
that I used it it would generate an EAccessViolation.
I changed the last loop (after // PROCESS INITIAL DATA RECEIVED) to the
following
which made the problem go away.
while (Stream->WaitForData (500)) {
memset (Buffer, 0, sizeof (Buffer));
if (Stream->Read (Buffer, sizeof (Buffer) - 1)>0) {
// PROCESS NEXT
}
}
What is causing this behaviour?
Aloha
Bill