socket 怎么理解?
发布于 7 年前 作者 MiYogurt 3222 次浏览 来自 问答

http 里面的 socket 到底是什么?一个 http 请求不就一个 socket 吗,socket 包括协议,(俩端)IP,(俩端)端口,持续请求的话这5项都没有改啊。继续请求不就复用了么,怎么来的第二个 socket?

然而 http 模块里面可以设置 maxSockets, maxFreeSockets,这个 socket 哪来的多个? 还是每个数据链路层通信包都有一个 socket ?

7 回复

就是聊天室啊

来自酷炫的 CNodeMD

@JustforNode socket 是什么我知道 但是 ,http.Agent 类 Agent 负责为 HTTP 客户端管理连接的持续与复用。 它为一个给定的主机与端口维护着一个等待请求的队列,且为每个请求重复使用一个单一的 socket 连接直到队列为空,此时 socket 会被销毁或被放入一个连接池中,在连接池中等待被有着相同主机与端口的请求再次使用。

image.png

这里的2个选项作用何在?

@MiYogurt 就是他写的字面意思吧,代理主机允许的最大连接量,如果maxSockets 是 10000,就最多只同时允许一万个客户端连到代理上

@JustforNode 恩,隔了一天去看,感觉就好多了,socket 是端到端,这里说的每个主机,指的可以是 C ,也可是是 S,但是无论哪一端的 socket 都是一样的。

Agent是对外发起http请求用的,跟接收请求没关系

Class: http.Agent# Added in: v0.3.4 An Agent is responsible for managing connection persistence and reuse for HTTP clients. It maintains a queue of pending requests for a given host and port, reusing a single socket connection for each until the queue is empty, at which time the socket is either destroyed or put into a pool where it is kept to be used again for requests to the same host and port. Whether it is destroyed or pooled depends on the keepAlive option.

回到顶部