Out of curiosity, what is the motivation for writing your own web server?  
There are a lot of them out there already.  It's non-trivial to write one that 
scales well, as you have to use asynchronous methods for it to scale (even a 
little) and they're not nearly as easy to grok.  There's a sample of an asynch 
server on MSDN:

http://msdn2.microsoft.com/en-us/library/fx6588te.aspx

Unfortunately for you, it doesn't have the problem you've got, because it uses 
a home-grown very much non-robust "protocol" to figure out when the data being 
sent has ended.

Back to your question -- there's no Accept method; I assume that you meant 
AcceptSocket.  You don't use that to "get all the data" -- as I think you know, 
you use the Socket that it returns to get the data, and there's no direct way 
to get it all.  (In theory, it could return a huge amount of data; if you're 
expecting a big transmission, you'll need to write it to disk as it comes in or 
you'll be in trouble.)

If you use BeginReceive, I don't believe that you'll have the problem -- once 
your callback has happened, either there's data or the other end has finished 
(by closing the socket, or by sending all that it should have sent according to 
the ContentLength header).  When you call EndReceive (in your callback method), 
it should return immediately (else the callback wouldn't have happened) and if 
it returns 0, you know you're done.  Look at this sample code:

http://msdn2.microsoft.com/en-us/library/w7wtt64b.aspx

If you decide not to use the asynch pattern (BeginReceive/EndReceive), you need 
to check more than just Available -- as you've seen, it can return 0 before all 
the data has arrived.   You need to make sure that the socket is "ready for 
reading" to know whether the 0 from Available is "genuine."  You can do this 
with the Poll (or Select) method.  But using the asynch pattern will make your 
code much more scalable, as you otherwise won't be easily able to handle 
multiple clients at once (unless you use multiple threads).

Good luck...

At 01:55 PM 3/28/2006, Mike Andrews wrote
>Guys,
>
>I've been working on development of a webserver and I want to know how I can
>know that I have all of the data for a request.  If I post a form using the
>form's enctype attribute, the form is divided up (files and all) as
>delimited sections.  However I don't seem to get all of the data.  I loop
>until the Socket.Available property returns 0, but that is not  the end of
>the data.  If I just sit for a couple seconds, more data appears and then
>more still.  How can I use a TCPListener blocking Accept() method to get all
>of the data and know that my request has completed?
>
>Thanks,
>Mike


J. Merrill / Analytical Software Corp

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to