G
Guest
I'm using .NET2.0 to connect to a remote networked device. The device
accepts commands terminated with CRLF characters and responds with multiple
lines of data each terminated with CRLF.
Following the issue of a command I am finding that I am not receiving all of
the data, it simply stops receiving.
I use the DataAvailable property to check for the existance of data before
attempting to read.
The following code snippet shows how I am reading the data:
TCPClient netClient;
NetworkStream netStream;
NetworkReader netReader;
// Connet TCPClient
....
// Get stream fromTCPClient
netStream = netClient.GetStream();
// Create the stream reader
netReader = new StreamReader(netStream);
while (!exit)
{
if (netClient != null && netClient.Connected && netStream.DataAvailable)
{
dataToRead = true;
}
if (dataToRead)
{
// There is data available, read it
data = netReader.ReadLine();
// Process data
...
}
}
I am finding that occasionally the DataAvailable property simply returns
false and then continuously returns false, therefore the ReadLine method is
never called and the code performs a timeout.
When the DataAvailable property returns false I can still issue a ReadLine
and data is returned but DataAvailable remains false.
I have had to resort to checking the StreamReader.Peek() response as well as
NetworkStream.DataAvailable. The behaviour of each of independently is
inconsistant.
The following code will read all data:
TCPClient netClient;
NetworkStream netStream;
NetworkReader netReader;
// Connet TCPClient
....
// Get stream fromTCPClient
netStream = netClient.GetStream();
// Create the stream reader
netReader = new StreamReader(netStream);
while (!exit)
{
if (netClient != null && netClient.Connected)
{
if (netStream.DataAvailable != 0 || netReader.Peek() != -1)
{
dataToRead = true;
}
}
if (dataToRead)
{
// There is data available, read it
data = netReader.ReadLine();
// Process data
...
}
}
Can anyone explain this ?
Regards
Steve
accepts commands terminated with CRLF characters and responds with multiple
lines of data each terminated with CRLF.
Following the issue of a command I am finding that I am not receiving all of
the data, it simply stops receiving.
I use the DataAvailable property to check for the existance of data before
attempting to read.
The following code snippet shows how I am reading the data:
TCPClient netClient;
NetworkStream netStream;
NetworkReader netReader;
// Connet TCPClient
....
// Get stream fromTCPClient
netStream = netClient.GetStream();
// Create the stream reader
netReader = new StreamReader(netStream);
while (!exit)
{
if (netClient != null && netClient.Connected && netStream.DataAvailable)
{
dataToRead = true;
}
if (dataToRead)
{
// There is data available, read it
data = netReader.ReadLine();
// Process data
...
}
}
I am finding that occasionally the DataAvailable property simply returns
false and then continuously returns false, therefore the ReadLine method is
never called and the code performs a timeout.
When the DataAvailable property returns false I can still issue a ReadLine
and data is returned but DataAvailable remains false.
I have had to resort to checking the StreamReader.Peek() response as well as
NetworkStream.DataAvailable. The behaviour of each of independently is
inconsistant.
The following code will read all data:
TCPClient netClient;
NetworkStream netStream;
NetworkReader netReader;
// Connet TCPClient
....
// Get stream fromTCPClient
netStream = netClient.GetStream();
// Create the stream reader
netReader = new StreamReader(netStream);
while (!exit)
{
if (netClient != null && netClient.Connected)
{
if (netStream.DataAvailable != 0 || netReader.Peek() != -1)
{
dataToRead = true;
}
}
if (dataToRead)
{
// There is data available, read it
data = netReader.ReadLine();
// Process data
...
}
}
Can anyone explain this ?
Regards
Steve