首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当客户端连接到'localhost‘上的服务时,为什么System.Net.ServicePoint.ConnectionLimit使用'7FFFFFFF’(Int32.MaxValue/2147483647)?

当客户端连接到'localhost‘上的服务时,为什么System.Net.ServicePoint.ConnectionLimit使用'7FFFFFFF’(Int32.MaxValue/2147483647)?
EN

Stack Overflow用户
提问于 2014-04-08 07:35:13
回答 1查看 3.4K关注 0票数 7

为什么System.Net.ServicePoint.ConnectionLimit在客户端连接到'localhost'上的服务时使用‘7FFFFFF’(Int32.MaxValue/2147483647),而如果该服务在远程计算机上运行,它决定使用'2‘作为默认值?

最初,我认为如果没有设置ServicePointManager.DefaultConnectionLimit,就会是servicepoint.connectionlimit。然而,我刚刚意识到(一旦我从客户那里得到一个问题),它是Int32.MaxValue/2147483647。

我做了一些研究(详情请参阅下面的链接),但我无法找出为什么它使用it 32.maxvalue。我可以猜测它可能是为了更好的性能,因为输入请求和响应消息没有跨越边界。

我的问题:

  1. 如果服务运行在'localhost'上,为什么是Int32.MaxValue?(英语中的任何解释;)我从反射器复制的代码片段也很好--就像我猜测的那样--但没有完全理解代码:)
  2. 我理解perf --但从'2‘(默认)到'int32.maxvalue’听起来很牵强。换句话说,为什么只要请求不跨网络打开那么多TCP连接就可以了。(换句话说,为什么默认为int32.maxvalue -它没有副作用)

与此相关的一些有用的链接:

How and where the TCP connection has been created in httpwebrequest, and how is it related to servicepoint?

http://blogs.microsoft.co.il/idof/2011/06/20/servicepointmanagerdefaultconnectionlimit-2-depends/

http://msdn.microsoft.com/en-us/library/system.net.servicepoint.connectionlimit(v=vs.110).aspx

http://arnosoftwaredev.blogspot.com/2006/09/net-20-httpwebrequestkeepalive-and.html

来自反射器的代码片段

代码语言:javascript
复制
  public int ConnectionLimit
        {
            get
            {
                if ((!this.m_UserChangedLimit && (this.m_IPAddressInfoList == null)) && (this.m_HostLoopbackGuess == TriState.Unspecified))
                {
                    lock (this)
                    {
                        if ((!this.m_UserChangedLimit && (this.m_IPAddressInfoList == null)) && (this.m_HostLoopbackGuess == TriState.Unspecified))
                        {
                            IPAddress address = null;
                            if (IPAddress.TryParse(this.m_Host, out address))
                            {
                                this.m_HostLoopbackGuess = IsAddressListLoopback(new IPAddress[] { address }) ? TriState.True : TriState.False;
                            }
                            else
                            {
                                this.m_HostLoopbackGuess = NclUtilities.GuessWhetherHostIsLoopback(this.m_Host) ? TriState.True : TriState.False;
                            }
                        }
                    }
                }
                if (!this.m_UserChangedLimit && !((this.m_IPAddressInfoList == null) ? (this.m_HostLoopbackGuess != TriState.True) : !this.m_IPAddressesAreLoopback))
                {
                    return 0x7fffffff;
                }
                return this.m_ConnectionLimit;
            }
            set
            {
                if (value <= 0)
                {
                    throw new ArgumentOutOfRangeException("value");
                }
                if (!this.m_UserChangedLimit || (this.m_ConnectionLimit != value))
                {
                    lock (this)
                    {
                        if (!this.m_UserChangedLimit || (this.m_ConnectionLimit != value))
                        {
                            this.m_ConnectionLimit = value;
                            this.m_UserChangedLimit = true;
                            this.ResolveConnectionLimit();
                        }
                    }
                }
            }
        }

致以敬意,

EN

回答 1

Stack Overflow用户

发布于 2014-04-08 08:29:23

for值只是一个没有限制的占位符。你应该能够与自己建立尽可能多的联系。

您粘贴的代码基本上只是检查是否连接到环回地址,如果是,则返回maxint,如果不是,则返回servicepoint.connectionlimit的值(默认情况下为2,但可以更改)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22930486

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档