所有请求如何使用同一端口连接到 Web 服务器?
Web 服务器如何使用相同的 TCP 连接端口 (80) 为其客户端提供服务。对于 UDP 连接,我知道本身没有连接,因此我们可以让多个客户端将数据包发送到同一端口。如果我尝试使用本地主机上已使用的端口,则会收到 BindException。
我看到的一个解决方案是为每个连接启动一个线程,但是对于像 google/yahoo 这样每个服务器中有 >100000 个连接的网站来说这不是很麻烦吗?
Web 服务器采用什么解决方案来解决这个问题?
How does a web-server serve its client using the same port(80) for a TCP connection. For a UDP connection, i understand that there is no connection, per se, so we can have multiple clients send packets to same port. If i try to use an already used port on my localhost, i get BindException.
One solution i see to this is starting a thread for each connection, but wouldnt this be cumbersome for site like google/yahoo where there a >100000 connections in each server?
What solutions do web servers employ for this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
服务器侦听众所周知的端口 (80),并在收到请求后将请求委托给工作套接字。这样它就可以服务下一个请求。您可以编写自己的简单服务器来了解发生了什么。 Oracle 网站有一个很好的示例代码。 [1]
[1] http://java.sun.com/developer /technicalArticles/Networking/Webserver/WebServer.java
首先它创建一个服务器套接字;
然后它在指定的端口上进行监听,并在接受请求后创建一个新的套接字;
如代码所示,它有一个工作线程池,因此在给定时刻您可以控制服务器在给定时间处理的请求数量。其他人可能在队列中等待。
Server listens on a well-known port (80) and delegate the request to a worker socket once it receive the request. That way it can serve the next request. You can write your own simple server to understand whats going on. Oracle site has a nice example code. [1]
[1] http://java.sun.com/developer/technicalArticles/Networking/Webserver/WebServer.java
first it creates a server socket;
then it listnes on the specified port and create a new socket once it accepts the request;
As shown in the code, it has a worker thread pool, so at a given moment you can control the number of request get served by the server at a given time. Others wait in a Queue may be.
您只有一个用于侦听的端口,但连接有两个端口,连接的每一侧都有一个端口。该配对必须是唯一的。
因此,假设您连接到 google.com 端口 80,那么您的连接将在您的计算机上有一些端口,例如 google.com 的 42312 和端口 80。您可以使用netstat -a查看您的连接。要获得较短的列表:netstat -an| grep ESTABLISHED" 显示所有已建立的连接,而不将其 IP 解析为名称。
You only have one port for listening, but a connection has two ports, one on each side of the connection. This pare must be unique.
So, say you connect to google.com port 80, then your connection will have some port on your machine, say 42312 and port 80 at google.com. You can see your connections with netstat -a. To get a shorter list: netstat -an| grep ESTABLISHED" Which shows all established connections without resolving their IPs to names.
AFAIK,Apache 将为每个请求启动一个新线程< /a>,这是像 Node.js 这样的事件驱动服务器成为 快一点。谷歌和雅虎也拥有大量服务器,并将如此大的处理负载分散在它们之间。罗杰所说的也有道理,尽管我不能 100% 确定 google 在端口 42312 上执行输出将如何到达您的计算机端口 80 的详细信息:P
AFAIK, Apache will start a new thread for every request, which is a big reason that event driven servers like Node.js are a little faster. Google and Yahoo also have TONS of servers and spread this large processing load among them. What Roger says also makes sense, although I'm not 100% sure on the details of how exactly google doing output on port 42312 would reach your computer at port 80 :P