I’m having a hard time figuring out while a HttpWebRequest in .NET 4.0 creates a socket connection for each request but isn’t able to close the socket afterwards.
When I execute e.g 50 HttpWebRequests I see 50 TCP socket connections in the WAIT state in netstat.
I already reduced the code to a minimum:
Dim webReq As HttpWebRequest
Dim webRes As HttpWebResponse
Dim webStream As IO.StreamReader
Dim _answer As String
webReq = System.Net.HttpWebRequest.Create(TextBox1.Text)
webReq.Timeout = 10000
webReq.Method = WebRequestMethods.Http.Get
webRes = webReq.GetResponse
webStream = New IO.StreamReader(webRes.GetResponseStream(), System.Text.Encoding.ASCII())
_answer = webStream.ReadToEnd
RichTextBox1.Text = _answer
webRes.Close()
webRes = Nothing
webReq = Nothing
Any idea, hint is appreciated!
Thanks in advance
Chris
Do you mean ‘TIME_WAIT’? If so, do your 50 requests, go for a coffee, come back to your box, recheck with netstat.
Closing a TCP socket performs the terminate handshake and queues the socket object for release. The socket is not actually released for some minutes by TCP design – some packets may still be on their way by slow routes even after a successful connection termination & so the socket must not be reallocated to another connection until any such leftovers have died.
Rgds,
Martin