I’m trying to use HttpWebRequest and HttpWebResponse in .NET 3.5, running them in asynchronously: BeginGetRequestStream, EndGetRequestStream, BeginWrite, EndWrite, BeginGetResponse, EndGetResponse, BeginRead, EndRead – all parts of handling a request are asynchronous.
I have a few threads that send a large number of concurrent requests. EndRead and EndWrite are both blocking operations – they block the current thread while the actual read/write against the stream is done, I’m trying to come up with an ideal input/output buffer size for these operations.
My reasoning is this: as I have multiple requests active at a time, they’ll keep firing callbacks to let the thread know there’s some data available or data was sent. If my buffers are large, reading/writing the data through the wire will take longer, so EndRead/EndWrite will block longer. This would force the other requests on the same thread to wait a bit longer, since their notifications will have to wait until the thread is unblocked.
So, my question is, what would be a good read / write buffer size in this situation. I was thinking 2048 bytes each, but some sample code I saw in various blogs show wildly different values.
Thanks in advance for any ideas.
I think a better solution would be not to worry about the buffer sizes too much, but don’t block the threads. If you pass a delegate to the
callbackparameter of theBegin*methods, that callback is executed when the operation completes and you can callEnd*from there, which will (almost) immediately return. No blocking necessary.And regarding the buffer sizes, if they really matter to you, you should profile and find out what works best in your specific situation.