C# – .Net C# TcpClient / Socket HTTP Client Performance / Efficiency

chttpnetsocketstcpclient

I'm writing an HTTP client using the .Net TcpClient / Sockets.

So far, the client handles both Content-Length and chunked responses by iterating through the NetworkStream response (after writing a GET request to the TcpClient), parsing the headers and retrieving the relevant message body bytes / chunked bytes. To do this it uses the NetworkStream ReadByte method.

This all works fine, but performance is a key consideration of the application so I would like to make it as quick and efficient as possible.

Initially this will involve swapping ReadByte for Read for the message body (based on Content-Length) or chunked message body byte retrieval into an appropriately sized buffer, using ReadByte in all other areas (such as reading the Headers, Chunk sizes etc).

I'm interested to know thoughts on better / different ways to do this to achieve optimum performance? Obviously the main problem with HTTP is not knowing the length of the response stream unless it is parsed as it is retrieved.

There a specific reasons why I'm not using more abstract classes (eg HttpWebRequest) for this (I need better control at the socket level).

Many Thanks,

Chris

Best Answer

I suggest using a process with a medium sized buffer. Repeatedly fill the buffer until the response stream ends. When the buffer is full, or the stream ends, attach that buffer content onto the string (or whatever you're using to store the message).

If you want to read an important bit of information early in the stream, read just enough of the stream to see that. (In other words, you don't need to fill the buffer on the first pass if you don't want to.)

You should also consider using an event system to signal the presence of new data, which has been shaped in such a way that the main part of your process doesn't need to know anything about where the data came from or how you are buffering it.

Edit

In response to your comment question, if you have one connection that you are trying to reuse for multiple requests, you would create a thread that reads from it over and over. When it finds data, it uses the event to push it out for the main part of your program to handle. I don't have a sample handy, but you should be able to find several with a few bing or google searches.

Related Topic