首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TCP连接是如何在httpwebrequest中创建的,以及它与servicepoint的关系如何?

TCP连接是如何在httpwebrequest中创建的,以及它与servicepoint的关系如何?
EN

Stack Overflow用户
提问于 2014-04-02 02:06:43
回答 1查看 2K关注 0票数 2

我试图找出在使用HttpWebRequest时何时建立了TCP连接,以及这些连接是如何使用ServicePoint池和重用的。

我看过system.dll,尝试使用ILSpy和Reflector浏览代码,不知怎么没有看到任何对套接字的引用,没有建立tcp连接等等。

下面我已经粘贴了解压缩代码-请给我一些提示或者重定向,这样我才能理解:

  1. 什么时候创建了TCP连接?
  2. 如何使用ServicePoint保持这些连接的存活、池化和重用?

代码片段来自HttpWebRequest of System.dll:

代码语言:javascript
复制
public override Stream GetRequestStream()
    {
        TransportContext context;
        return this.GetRequestStream(out context);
    }

    public Stream GetRequestStream(out TransportContext context)
    {
        if (Logging.On)
        {
            Logging.Enter(Logging.Web, this, "GetRequestStream", "");
        }
        context = null;
        this.CheckProtocol(true);
        if ((this._WriteAResult == null) || !this._WriteAResult.InternalPeekCompleted)
        {
            lock (this)
            {
                if (this._WriteAResult != null)
                {
                    throw new InvalidOperationException(SR.GetString("net_repcall"));
                }
                if (this.SetRequestSubmitted())
                {
                    throw new InvalidOperationException(SR.GetString("net_reqsubmitted"));
                }
                if (this._ReadAResult != null)
                {
                    throw ((Exception) this._ReadAResult.Result);
                }
                this._WriteAResult = new LazyAsyncResult(this, null, null);
                this.Async = false;
            }
            this.CurrentMethod = this._OriginVerb;
            while (this.m_Retry && !this._WriteAResult.InternalPeekCompleted)
            {
                this._OldSubmitWriteStream = null;
                this._SubmitWriteStream = null;
                this.BeginSubmitRequest();
            }
            while (this.Aborted && !this._WriteAResult.InternalPeekCompleted)
            {
                if (!(this._CoreResponse is Exception))
                {
                    Thread.SpinWait(1);
                }
                else
                {
                    this.CheckWriteSideResponseProcessing();
                }
            }
        }
        ConnectStream connectStream = this._WriteAResult.InternalWaitForCompletion() as ConnectStream;
        this._WriteAResult.EndCalled = true;
        if (connectStream == null)
        {
            if (Logging.On)
            {
                Logging.Exception(Logging.Web, this, "EndGetRequestStream", this._WriteAResult.Result as Exception);
            }
            throw ((Exception) this._WriteAResult.Result);
        }
        context = new ConnectStreamContext(connectStream);
        if (Logging.On)
        {
            Logging.Exit(Logging.Web, this, "GetRequestStream", connectStream);
        }
        return connectStream;
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-02 10:19:03

在浏览了一段时间的代码之后,我想我有点理解抽象了。基本上,servicepoint、servicepoint管理器、tcp连接是如何创建的、连接已被池化、排队等总是让我感到困惑。下面的信息帮助了我--希望这对好奇或试图理解这些细节的人有用:

FindServicePoint(string ServicePoint :对特定主机(目的主机Ip:端口)的“连接”的高级抽象(这就是为什么在servicePointManger中定义了ex函数静态ServicePoint,int端口)。

ServicePointManager:作为名称表示它的全局(静态)类管理服务点。

连接(内部类):基本上,我认为这是一个代表TCP连接的连接。它基本上来自于System.Net.PoolStream (内部类-它有其使用的套接字的定义),它是从流派生的。

ConnectionGroup (内部类):每个HttpWebRequest与一个连接组相关联。(基本上它创建的connectionLimit最多是基于connectionLimit (可以通过ServicePointManager进行全局配置,也可以使用其servicePoint属性配置每个creates请求)每个httpwebrequest的连接对象数量)

如果达到连接限制,它就会简单地排队并传递到线路(很有可能--但仍然没有得到这样做的代码)。

如果要连接到本地机器上的服务,则servicepoint.connectionlimit不再等于servicepointmanager.defaultconnectionlimit。它默认为;int.Maxvalue (21474836477FFFFFFFF)(您可以参考:http://blogs.microsoft.co.il/idof/2011/06/20/servicepointmanagerdefaultconnectionlimit-2-depends/ )。

更新:

以下两个链接看起来也很有用:

System.Net.ServicePointManager.DefaultConnectionLimit and .MaxServicePointIdleTime

http://blogs.msdn.com/b/jpsanders/archive/2009/05/20/understanding-maxservicepointidletime-and-defaultconnectionlimit.aspx

诚挚的问候!

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

https://stackoverflow.com/questions/22800216

复制
相关文章

相似问题

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