简体   繁体   English

如何从 Java 中的端口提供 HTTP 内容

[英]How to serve HTTP content from a port in Java

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/ .当我通过在其 bin/ 目录中执行批处理文件来运行 ActiveMQ 时,我可以通过打开 Web 浏览器并转到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.这是我的本地沙箱,我没有安装任何网络服务器( httpd或其他)。 So how is it that ActiveMQ is able to "register" a port on my machine, and listen to it exclusively?那么,ActiveMQ 是如何在我的机器上“注册”一个端口并专门监听它的呢?

If I try to go to http://localhost:8162/admin/ (notice the different port #), I get an unable to connect error.如果我尝试访问http://localhost:8162/admin/ (注意不同的端口号),我会收到无法连接错误。

Somewhere, somehow, AMQ is saying "map this URI ( localhost:8161 ) to some root directory on this machine".在某个地方,不知何故,AMQ 说“将此 URI ( localhost:8161 ) 映射到这台机器上的某个根目录”。 As a programmer, I'm interested in how something like this works.作为一名程序员,我对这样的事情如何工作很感兴趣。

A Java process is able to use any port (>= 1024 on linux) as a web server or for any other purpose. Java 进程能够使用任何端口(Linux 上 >= 1024)作为 Web 服务器或用于任何其他目的。 You don't need a separate web server to do this您不需要单独的 Web 服务器来执行此操作

I suggest you read up on sockets: here .我建议你阅读套接字: 这里 All a web server is, is a socket listener that handles the HTTP protocol. Web 服务器就是一个处理 HTTP 协议的套接字侦听器。 HTTP protocol is here . HTTP 协议在这里

Web servers often handle a lot of other things, but that is the basics. Web 服务器通常会处理很多其他事情,但这是最基本的。 If you want a small program that also runs a web server I suggest not re-inventing the wheel.如果您想要一个还运行 Web 服务器的小程序,我建议您不要重新发明轮子。 Try incorporating jetty into your server.尝试将jetty合并到您的服务器中。

ActiveMQ starts an embedded Jetty server , which listens for HTTP connections on that port. ActiveMQ 启动一个嵌入式Jetty 服务器,它侦听该端口上的 HTTP 连接。 You don't need any other server running.您不需要运行任何其他服务器。 It's all done from Java.这一切都是从Java完成的。 If you dig down deep enough, you'll find some variety of ServerSocket at the bottom of it all.如果你深入挖掘,你会在它的底部发现一些各种各样的ServerSocket You can learn all about sockets and listening on ports in the Java Tutorial .您可以在Java 教程中了解有关套接字和侦听端口的所有信息。

At its simplest level, ActiveMQ is creating a ServerSocket instance within itself and listening for connections using this server socket.在最简单的层面上,ActiveMQ 在其自身内创建一个 ServerSocket 实例并使用该服务器套接字监听连接。 A socket is always bound to a port.套接字始终绑定到端口。

One: this port is greater than (or equal to) 1024, so it means a "non root" user can listen on it.一:这个端口大于(或等于)1024,所以这意味着“非root”用户可以监听它。

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).这意味着 ActiveMQ 可能只在 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.尝试看看您是否可以从外部接口的 IP 地址打开该 URL:您可能无法打开。

If you are under a Unix system, you can check what program listens on which port by using netstat -ltpn .如果您在 Unix 系统下,您可以使用netstat -ltpn来检查哪个程序在哪个端口上侦听。

The basic system call for binding to a port is listen(2) .绑定到端口的基本系统调用是listen(2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM