I have a strange situation in my .NET CF 3.5 Windows Mobile 6.5 application. I have 2 threads.
In 1st thread I do following:
try
{
String url = "http://myserverurl";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
_currentRequest = request;
request.Timeout = 10000;
response = (HttpWebResponse)request.GetResponse();
ConnectionStatus connStatus = response.StatusCode == HttpStatusCode.OK;
response.Close();
}
catch (E开发者_Go百科xception e)
{
//log e
}
finally
{
}
In 2n thread a call a WebService through a SoapHttpClientProtocol based class generated by WebService reference.
soapClient.Url = "http://myserverurl";
soapClient.MethodOnWebService();
In both cases the url is the same. The 1st thread is used for connection checking purpose. It does the WebRequest periodically to check whetrher the server is available and displays the connection status (not shown in code). 2nd thread calls WebService on the same server (url). I observed, that when one thread is executing a WebRequest the 2nd one gets blocked or event timeouted while executing a Web Method. They both look as interfering each other. Why? I wonder if windows mobile network stack simply creates only one socket connection for both threads if it realizes that both goes to the same target IP:port? What about sessions? On desktop Windows I would expect 2 sessions being created and at least 2 sockets on client machine. Does anybody have any hints on how Windows Mobile (or .NET CF) manages connections and socket reusage?
Regards
I would guess that there is a third session somewhere. What you're seeing is most likely due to a little-known (until it bites you, like now) recommended connection-limitation in the HTTP-protocol. Section 8.1.4 of RFC2068 says "A single-user client SHOULD maintain AT MOST 2 connections with any server or proxy". I've experienced the same limitation myself, most recently on Windows Phone 7.
The limit lies in the WebRequest and the solution is to increase the limit:
// set connection limit to 5
ServicePointManger.DefaultConnectionLimit = 5;
See e.g. this old blog entry from David Kline.
精彩评论