Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Newbie: The best way to detect the end of a data stream...

Newbie: The best way to detect the end of a data stream...

Scheduled Pinned Locked Moved C#
regexquestionsysadminhelp
3 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Phillip Hodges
    wrote on last edited by
    #1

    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"

    N S 2 Replies Last reply
    0
    • P Phillip Hodges

      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"

      N Offline
      N Offline
      Nader Elshehabi
      wrote on last edited by
      #2

      Hello keep reading until NetworkStream.DataAvailable becomes false.

      Regards:rose:

      1 Reply Last reply
      0
      • P Phillip Hodges

        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"

        S Offline
        S Offline
        Scott Dorman
        wrote on last edited by
        #3

        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;

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups