Http request
-
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? -
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?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
-
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
Yes, I ended up trying some thimg very similar
do { 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