Newbie: The best way to detect the end of a data stream...
-
Hello all.. I am trying to construct a way of extracting data from a network stream. However I am running into a small problem. I am using regex to detect a pair of return & newlines "\r\n\r\n". However some of the messages received have "\r\n\r\n" in the middle of the stream. So some of the messages are cut off halfway.
byte[] ami_response_buffer = new byte[ami_buffer_size]; MemoryStream ami_memory_stream = new MemoryStream(); string ami_response = ""; int ami_bytes_read = 0; do { ami_bytes_read = ami_client_stream.Read(ami_response_buffer, 0, ami_buffer_size); ami_memory_stream.Write(ami_response_buffer, 0, ami_bytes_read); ami_response = Encoding.ASCII.GetString(ami_memory_stream.GetBuffer()); // Check for end of message if (Regex.Match(ami_response, "\r\n\r\n", RegexOptions.IgnoreCase).Success) { ami_bytes_read = 0; } } while (ami_bytes_read > 0); return ami_response;
What is the best way to detect the end of a data stream? Thanks in advance... Phil"Rules are for the obedience of fools and the guidance of wise men"
-
Hello all.. I am trying to construct a way of extracting data from a network stream. However I am running into a small problem. I am using regex to detect a pair of return & newlines "\r\n\r\n". However some of the messages received have "\r\n\r\n" in the middle of the stream. So some of the messages are cut off halfway.
byte[] ami_response_buffer = new byte[ami_buffer_size]; MemoryStream ami_memory_stream = new MemoryStream(); string ami_response = ""; int ami_bytes_read = 0; do { ami_bytes_read = ami_client_stream.Read(ami_response_buffer, 0, ami_buffer_size); ami_memory_stream.Write(ami_response_buffer, 0, ami_bytes_read); ami_response = Encoding.ASCII.GetString(ami_memory_stream.GetBuffer()); // Check for end of message if (Regex.Match(ami_response, "\r\n\r\n", RegexOptions.IgnoreCase).Success) { ami_bytes_read = 0; } } while (ami_bytes_read > 0); return ami_response;
What is the best way to detect the end of a data stream? Thanks in advance... Phil"Rules are for the obedience of fools and the guidance of wise men"
Hello keep reading until
NetworkStream.DataAvailable
becomes false.Regards:rose:
-
Hello all.. I am trying to construct a way of extracting data from a network stream. However I am running into a small problem. I am using regex to detect a pair of return & newlines "\r\n\r\n". However some of the messages received have "\r\n\r\n" in the middle of the stream. So some of the messages are cut off halfway.
byte[] ami_response_buffer = new byte[ami_buffer_size]; MemoryStream ami_memory_stream = new MemoryStream(); string ami_response = ""; int ami_bytes_read = 0; do { ami_bytes_read = ami_client_stream.Read(ami_response_buffer, 0, ami_buffer_size); ami_memory_stream.Write(ami_response_buffer, 0, ami_bytes_read); ami_response = Encoding.ASCII.GetString(ami_memory_stream.GetBuffer()); // Check for end of message if (Regex.Match(ami_response, "\r\n\r\n", RegexOptions.IgnoreCase).Success) { ami_bytes_read = 0; } } while (ami_bytes_read > 0); return ami_response;
What is the best way to detect the end of a data stream? Thanks in advance... Phil"Rules are for the obedience of fools and the guidance of wise men"
I'm assuming that "\r\n\r\n" is the marker you are trying to use to indicate the end of the stream. You would want to do something like this:
string ami_response = "";
// Check to see if this NetworkStream is readable.
if (ami_client_stream.CanRead)
{
byte[] ami_response_buffer = new byte[ami_buffer_size];
MemoryStream ami_memory_stream = new MemoryStream();
int ami_bytes_read = 0;myCompleteMessage = new StringBuilder(); // Incoming message may be larger than the buffer size. do { ami\_bytes\_read = ami\_client\_stream.Read(ami\_response\_buffer, 0, ami\_response\_buffer.Length); myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(ami\_response\_buffer, 0, ami\_bytes\_read)); } while (ami\_client\_stream.DataAvailable); ami\_response = myCompleteMessage.ToString();
}
return ami_response;