网络服务器套接字 [英] Web server socket

查看:49
本文介绍了网络服务器套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当客户端向 Web 服务器发送请求时.Web 服务器是否打开一个新的套接字?或者是用于新请求的现有开放套接字?.如果它确实为每个新请求打开了一个套接字,那么这是如何管理的,因为 Http 是一个无状态协议.对于所有 Web 服务器也是如此,还是不​​同的 Web 服务器对此有不同的处理方式?.

When a client sends a request to web server. Does the web server open a new socket ?. Or is an existing open socket used for the new request ?. If it does open a socket for each new request, how is this managed since Http is a stateless protocol. Also is this same for all web servers or do different web servers handle this differently ?.

推荐答案

使用 Unix 术语(这几乎是通用的——套接字是在 Unix 的 BSD 风格中引入的,并从那里传播到任何地方),几乎任何 TCP 网络服务器(web- 或其他方式)将对绑定到众所周知的端口"(通常但不一定是 HTTP 服务器的端口 80).当客户端连接时,会通知服务器(以操作系统相关的方式)然后它可以在侦听套接字上执行 accept,这会创建一个新的插座.

Using Unix terminology (which is near universal -- sockets were introduced in the BSD flavor of Unix and spread everywhere from there), pretty much any TCP network server (web- or otherwise) will have done a listen on a socket bound to a "well-known port" (typically but not necessarily port 80 for HTTP servers). When a client connects, the server is notified (in OS-dependent ways) and it can then do an accept on the listening socket, which does create a new socket.

根据使用的 HTTP 协议的级别(正常的 1.1,或旧的但仍在使用的 1.0)和请求中的一些标头,客户端可能会要求一个一次性套接字(它将只处理一个请求和一个响应),或者,现在更常见的是持久的(也称为,旧术语可以追溯到 HTTP 1.0 后期,作为保持活动"连接).服务器不必满足客户端对持久连接的请求,但通常会尝试这样做,因为它使客户端性能好得多.每个服务器肯定会选择自己的启发式方法来判断何时负载过重(同时传入的请求过多)来满足持久连接的请求.

Depending on the level of HTTP protocol in use (the normal 1.1, or the old but still used 1.0) and some headers in the request, the client may be asking for a one-use socket (which will deal with just one request and one response), or, more commonly these days, a persistent one (also known, with older terminology dating to late HTTP 1.0 days, as a "keep alive" connection). The server does not have to honor the client's request for the connection to be persistent, but typically tries to because it makes client performance SO much better. Each server may definitely pick their own heuristics about when it's too loaded (too many requests coming in at once) to honor requests for persistent connections.

即使在使用持久连接时,HTTP 仍然是无状态的——客户端可能会在仍然打开的套接字上发出不同的请求,和/或尝试打开不同的套接字,而 HTTP 只是分别处理每个请求/响应对.套接字的持久性仅在 TCP 握手方面节省时间 &c(因为 HTTP 确实在 TCP 之上工作,所以每个新的 TCP 连接都需要自己的握手,&c).

HTTP is still stateless even when persistent connections are in use -- the client may make different requests on the still-open socket, and/or try opening different sockets, and HTTP just deals with each request/response pair separately. The socket's persistence only saves time in terms of TCP handshakes &c (since HTTP does work on top of TCP, each new TCP connection requires its own handshake, &c).

这篇关于网络服务器套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆