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. Http request

Http request

Scheduled Pinned Locked Moved C#
sysadmintutorialquestion
3 Posts 2 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.
  • S Offline
    S Offline
    Steve Messer
    wrote on last edited by
    #1

    I am trying to write a small web server that will receive POST's. The data being sent to me does not include length in the header so I just need to read the socket until it no longer has any data. I have tried setting it up by adding this to my current code. I started with the following which works for small items. Int32 bytesRead = sock.Receive(byReceive, byReceive.Length, 0); Then I added a while loop. Int32 bytesRead = sock.Receive(byReceive, byReceive.Length, 0); while( bytesRead > 0 ) { bytesRead = sock.Receive(byReceive, byReceive.Length, 0); } I have looked at lots of examples that are either too simple or too complex. I never get out of the while loop. Anyone have any example code or know where one is?

    S 1 Reply Last reply
    0
    • S Steve Messer

      I am trying to write a small web server that will receive POST's. The data being sent to me does not include length in the header so I just need to read the socket until it no longer has any data. I have tried setting it up by adding this to my current code. I started with the following which works for small items. Int32 bytesRead = sock.Receive(byReceive, byReceive.Length, 0); Then I added a while loop. Int32 bytesRead = sock.Receive(byReceive, byReceive.Length, 0); while( bytesRead > 0 ) { bytesRead = sock.Receive(byReceive, byReceive.Length, 0); } I have looked at lots of examples that are either too simple or too complex. I never get out of the while loop. Anyone have any example code or know where one is?

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      Did you try using the Available[^] property instead of (bytesRead > 0)? Also, make sure that the last Receive requests exactly Available bytes, otherwise the socket will block trying to receive byReceive.Length.

      int bytesToRead = 0;
      while ((bytesRead = sock.Available) != 0)
      {
      int bytesToRead = Math.Min(bytesRead, byReceive.Length);
      sock.Receive(byReceive, bytesToRead, 0);
      }

      Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      S 1 Reply Last reply
      0
      • S S Senthil Kumar

        Did you try using the Available[^] property instead of (bytesRead > 0)? Also, make sure that the last Receive requests exactly Available bytes, otherwise the socket will block trying to receive byReceive.Length.

        int bytesToRead = 0;
        while ((bytesRead = sock.Available) != 0)
        {
        int bytesToRead = Math.Min(bytesRead, byReceive.Length);
        sock.Receive(byReceive, bytesToRead, 0);
        }

        Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        S Offline
        S Offline
        Steve Messer
        wrote on last edited by
        #3

        Yes, I ended up trying some thimg very similardo { bytesRead = sock.Receive(byReceive, byReceive.Length, 0); totalRead += bytesRead; Thread.Sleep(100); strRetPage = strRetPage + Encoding.ASCII.GetString(byReceive, 0, bytesRead); } while( sock.Available > 0 );
        Now the problem is that without the Sleep it reads the socket once and bails out. Another problem is that 10% of the time the socket claims to be empty and receives nothing. I have little hair left to pull out :) -- modified at 21:17 Thursday 22nd December, 2005

        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