如何从 Java 端口提供 HTTP 内容
当我通过执行 bin/ 目录中的批处理文件来运行 ActiveMQ 时,我可以通过打开 Web 浏览器并访问 http://localhost:8161/admin/
。
这让我很好奇。
这是我的本地沙箱,我没有安装任何 Web 服务器(httpd
或其他)。那么 ActiveMQ 是如何能够在我的机器上“注册”一个端口并专门监听它的呢?
如果我尝试访问 http://localhost:8162/admin/
(注意不同的端口号),我会收到无法连接错误。
在某个地方,不知何故,AMQ 说“将此 URI (localhost:8161
) 映射到本机上的某个根目录”。作为一名程序员,我对这样的事情如何工作很感兴趣。
When I run ActiveMQ by executing the batch file in its bin/ directory, I am able to go to its admin/management console by opening a web browser and going to http://localhost:8161/admin/
.
This has me curious.
This is my local sandbox and I do not have any web server (httpd
or otherweise) installed. So how is it that ActiveMQ is able to "register" a port on my machine, and listen to it exclusively?
If I try to go to http://localhost:8162/admin/
(notice the different port #), I get an unable to connect error.
Somewhere, somehow, AMQ is saying "map this URI (localhost:8161
) to some root directory on this machine". As a programmer, I'm interested in how something like this works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Java 进程能够使用任何端口(Linux 上 >= 1024)作为 Web 服务器或用于任何其他目的。您不需要单独的网络服务器来执行此操作
A Java process is able to use any port (>= 1024 on linux) as a web server or for any other purpose. You don't need a separate web server to do this
我建议您阅读有关套接字的内容:此处。 Web 服务器就是一个处理 HTTP 协议的套接字侦听器。 HTTP 协议位于此处。
Web 服务器通常处理许多其他事情,但这只是基础。如果你想要一个也运行网络服务器的小程序,我建议不要重新发明轮子。尝试将 jetty 合并到您的服务器中。
I suggest you read up on sockets: here. All a web server is, is a socket listener that handles the HTTP protocol. HTTP protocol is here.
Web servers often handle a lot of other things, but that is the basics. If you want a small program that also runs a web server I suggest not re-inventing the wheel. Try incorporating jetty into your server.
ActiveMQ 启动嵌入式 Jetty 服务器,该服务器侦听该端口上的 HTTP 连接。您不需要运行任何其他服务器。这一切都是由 Java 完成的。如果您深入挖掘,您会发现各种 ServerSocket 在这一切的底部。您可以在 Java 教程 中了解有关套接字和侦听端口的所有信息。
ActiveMQ starts an embedded Jetty server, which listens for HTTP connections on that port. You don't need any other server running. It's all done from Java. If you dig down deep enough, you'll find some variety of ServerSocket at the bottom of it all. You can learn all about sockets and listening on ports in the Java Tutorial.
在最简单的层面上,ActiveMQ 在自身内部创建一个 ServerSocket 实例,并使用该服务器套接字侦听连接。套接字始终绑定到端口。
At its simplest level, ActiveMQ is creating a ServerSocket instance within itself and listening for connections using this server socket. A socket is always bound to a port.
一:该端口大于(或等于)1024,因此意味着“非root”用户可以监听它。
第二:您只能绑定到专用地址的端口。这意味着 ActiveMQ 可能只在 127.0.0.1 (localhost) 上打开该端口。尝试看看是否可以从外部接口的 IP 地址打开该 URL:很可能不能。
如果您在Unix系统下,可以使用
netstat -ltpn
查看哪些程序监听了哪个端口。绑定到端口的基本系统调用是
listen(2)
。One: this port is greater than (or equal to) 1024, so it means a "non root" user can listen on it.
Second: you can bind to ports from dedicated addresses only. This means ActiveMQ may have only opened that port on 127.0.0.1 (localhost). Try and see if you can open that URL from your external interface's IP address: chances are you cannot.
If you are under a Unix system, you can check what program listens on which port by using
netstat -ltpn
.The basic system call for binding to a port is
listen(2)
.