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. How to avoid Stream.Read() block when internet connection is suddenly down?

How to avoid Stream.Read() block when internet connection is suddenly down?

Scheduled Pinned Locked Moved C#
tutorialquestion
8 Posts 4 Posters 0 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.
  • C Offline
    C Offline
    Chesnokov Yuriy
    wrote on last edited by
    #1

    I send web requests and read data web response:

    using (Stream stream = response.GetResponseStream())
    {
    Data = new byte[response.ContentLength];
    int offset = 0;
    int count = (int)response.ContentLength;
    int nBytes = 0;
    int nTotalBytes = 0;
    while ((nBytes = stream.Read(Data, offset, count)) > 0)
    {
    offset += nBytes;
    count -= nBytes;
    nTotalBytes += nBytes;
    }
    }

    If internet connection is suddenyl down Read() function blocks forever. Is there a way to avoid it using the same Read() method?

    Чесноков

    B L J 3 Replies Last reply
    0
    • C Chesnokov Yuriy

      I send web requests and read data web response:

      using (Stream stream = response.GetResponseStream())
      {
      Data = new byte[response.ContentLength];
      int offset = 0;
      int count = (int)response.ContentLength;
      int nBytes = 0;
      int nTotalBytes = 0;
      while ((nBytes = stream.Read(Data, offset, count)) > 0)
      {
      offset += nBytes;
      count -= nBytes;
      nTotalBytes += nBytes;
      }
      }

      If internet connection is suddenyl down Read() function blocks forever. Is there a way to avoid it using the same Read() method?

      Чесноков

      B Offline
      B Offline
      BobJanova
      wrote on last edited by
      #2

      It won't block forever, only until your network stack notices that the stream is no longer valid. But that can be quite a long time :laugh: . The short answer is no, Read is a blocking call. If you want to protect yourself from this possibility, you should do I/O in a background thread, or use asynchronous socket calls (Begin/EndRead etc).

      1 Reply Last reply
      0
      • C Chesnokov Yuriy

        I send web requests and read data web response:

        using (Stream stream = response.GetResponseStream())
        {
        Data = new byte[response.ContentLength];
        int offset = 0;
        int count = (int)response.ContentLength;
        int nBytes = 0;
        int nTotalBytes = 0;
        while ((nBytes = stream.Read(Data, offset, count)) > 0)
        {
        offset += nBytes;
        count -= nBytes;
        nTotalBytes += nBytes;
        }
        }

        If internet connection is suddenyl down Read() function blocks forever. Is there a way to avoid it using the same Read() method?

        Чесноков

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        As Bob already said, Read() is blocking. As the operation can be long-winding due to the amount of data, the lack of speed of the connection, or the potential failure of the transmission, you should be doing this on a background thread, allowing your app to continue its normal operation (at the bare minimum take care of the GUI), and possibly also allowing you to give up and maybe retry. BTW: I don't think every data source has to know and provide a ContentLength beforehand, so your approach with a pre-allocated byte array may fail for some sources. The alternative is to capture the data in a more dynamic structure (e.g. a list of arrays, each array holding one Read result) and then rearrange it if necessary. You could use this scheme when ContentLength returns abnormal values, such as zero. :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
        Please use <PRE> tags for code snippets, they improve readability.
        CP Vanity has been updated to V2.4

        C 1 Reply Last reply
        0
        • L Luc Pattyn

          As Bob already said, Read() is blocking. As the operation can be long-winding due to the amount of data, the lack of speed of the connection, or the potential failure of the transmission, you should be doing this on a background thread, allowing your app to continue its normal operation (at the bare minimum take care of the GUI), and possibly also allowing you to give up and maybe retry. BTW: I don't think every data source has to know and provide a ContentLength beforehand, so your approach with a pre-allocated byte array may fail for some sources. The alternative is to capture the data in a more dynamic structure (e.g. a list of arrays, each array holding one Read result) and then rearrange it if necessary. You could use this scheme when ContentLength returns abnormal values, such as zero. :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
          Please use <PRE> tags for code snippets, they improve readability.
          CP Vanity has been updated to V2.4

          C Offline
          C Offline
          Chesnokov Yuriy
          wrote on last edited by
          #4

          I already do the reading in the thread. The problem discovered with wifi internet connection type. Once the wifi is off Read blocks. How can I return from the background worker if it is blocked in the Read() method call? It was supposed to quit the thread if any error happen as it is with internet plugged into LAN. But wireless somehow blocks the method.

          Чесноков

          L 1 Reply Last reply
          0
          • C Chesnokov Yuriy

            I send web requests and read data web response:

            using (Stream stream = response.GetResponseStream())
            {
            Data = new byte[response.ContentLength];
            int offset = 0;
            int count = (int)response.ContentLength;
            int nBytes = 0;
            int nTotalBytes = 0;
            while ((nBytes = stream.Read(Data, offset, count)) > 0)
            {
            offset += nBytes;
            count -= nBytes;
            nTotalBytes += nBytes;
            }
            }

            If internet connection is suddenyl down Read() function blocks forever. Is there a way to avoid it using the same Read() method?

            Чесноков

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            The is no way for TCP to recognize that the other end no longer exists when it is waiting for a response. So the same is true for any protocol built on it. There might be situations where some sort of exception results. Maybe. For example I am not sure what the windows IP stack might do if you pull your network cable. But if you want to deal with all situations and not a just a few then you need to set up a timeout. Either via the protocol itself or via a secondary thread.

            C 1 Reply Last reply
            0
            • J jschell

              The is no way for TCP to recognize that the other end no longer exists when it is waiting for a response. So the same is true for any protocol built on it. There might be situations where some sort of exception results. Maybe. For example I am not sure what the windows IP stack might do if you pull your network cable. But if you want to deal with all situations and not a just a few then you need to set up a timeout. Either via the protocol itself or via a secondary thread.

              C Offline
              C Offline
              Chesnokov Yuriy
              wrote on last edited by
              #6

              jschell wrote:

              But if you want to deal with all situations and not a just a few then you need to set up a timeout. Either via the protocol itself or via a secondary thread

              I do set timeout in web request but I do not know how to set it for Read() operation as it just blocks.

              Чесноков

              1 Reply Last reply
              0
              • C Chesnokov Yuriy

                I already do the reading in the thread. The problem discovered with wifi internet connection type. Once the wifi is off Read blocks. How can I return from the background worker if it is blocked in the Read() method call? It was supposed to quit the thread if any error happen as it is with internet plugged into LAN. But wireless somehow blocks the method.

                Чесноков

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Have you tried setting Stream.ReadTimeout? Not sure if it will help, but I think it should. BTW: don't read Stream.ContentLength twice, defensive programming dictates you read it once and store it in a local variable. If it were to change, for whatever reason, your code could be in a lot of trouble. :)

                Luc Pattyn [My Articles] Nil Volentibus Arduum

                The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
                Please use <PRE> tags for code snippets, they improve readability.
                CP Vanity has been updated to V2.4

                C 1 Reply Last reply
                0
                • L Luc Pattyn

                  Have you tried setting Stream.ReadTimeout? Not sure if it will help, but I think it should. BTW: don't read Stream.ContentLength twice, defensive programming dictates you read it once and store it in a local variable. If it were to change, for whatever reason, your code could be in a lot of trouble. :)

                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                  The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
                  Please use <PRE> tags for code snippets, they improve readability.
                  CP Vanity has been updated to V2.4

                  C Offline
                  C Offline
                  Chesnokov Yuriy
                  wrote on last edited by
                  #8

                  yes, thanks, that was the variable to set to, stream.TimeOut by default it is 5 minutes to read and write, quite large value. I would never discover the problem had it not been to wifi connection. It is surprising that ordinary landline disconnection immediatly result in Read() failure but with wifi it waits entire timeout.

                  Чесноков

                  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