简体   繁体   English

将域分配给IP地址

[英]Assign domain to IP address

我该如何使我的Java应用程序在某个套接字(例如172.16.1.10:8080)上运行HTTP服务器并进行设置,以便当网络上的另一台计算机连接到域(例如http://myjavadomain.com )时被重定向到套接字?

If you are using Apache Tomcat then the below configuration will helpful to you. 如果您使用的是Apache Tomcat,则以下配置将对您有所帮助。

Fot Apache Tomcat you have to make on Host entry in the configuration location of the TOMCAT_HOME location. 对于Apache Tomcat,您必须在TOMCAT_HOME位置的配置位置中的Host条目上输入。

Follow below steps that will be helpful to you 请按照以下对您有帮助的步骤

1) Find the server.xml file in the conf location of TOMCAT_HOME 2) in the server.xml file make the below host entry 1)在TOMCAT_HOMEconf位置中找到server.xml文件。2)在server.xml文件中,输入以下主机条目

<Host name="www.xyz.com" debug="0" appBase="webapps/mynewhost" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
    <Logger className="org.apache.catalina.logger.FileLogger" directory="logs"  prefix="mynewhost_log." suffix=".txt" timestamp="true"/>
</Host>


In appBase property place the location of your web app 

3) Now in browser open the above url. 3)现在,在浏览器中打开上面的URL。

If you want to run a fully fledged HTTP server then you will probably want to use some external library. 如果要运行完全成熟的HTTP服务器,则可能要使用一些外部库。 For instance, Tomcat is written in Java, but there is also SUN's httpserver package . 例如, Tomcat用Java编写,但是还有SUN的httpserver软件包 If it's just a simple socket server you're after, you can use the built-in classes from the java.net package: 如果您只是一个简单的套接字服务器,则可以使用java.net包中的内置类:

ServerSocket server = new ServerSocket(8080);

while (running) {
    Socket socket = server.accept();
    handleConnection(socket);
}

This will listen for incoming socket connections on port 8080 and create a new Socket whenever a client connects. 这将侦听端口8080上的传入套接字连接,并在客户端连接时创建一个新的Socket You can then communicate with the client through the Socket 's InputStream and OuputStream , which you would probably do in a separate Thread, so that your ServerSocket can continue listening for incoming connections from other clients. 然后,您可以通过SocketInputStreamOuputStream与客户端进行通信,您可能会在单独的线程中进行此操作,以便ServerSocket可以继续侦听来自其他客户端的传入连接。

As for the second part of your question: by default, a web browser will connect to port 80, and there are several ways you could do port forwarding. 至于问题的第二部分:默认情况下,Web浏览器将连接到端口80,您可以通过多种方式进行端口转发。 One possible solution using iptables is given on this website : 该网站提供了一种使用iptables的可能解决方案:

iptables -t nat -I PREROUTING --src 0/0 --dst 172.16.1.10 -p tcp --dport 80 -j REDIRECT --to-ports 8080

But the easiest solution would be to just specify the port number directly when connecting to your machine, eg 但是最简单的解决方案是在连接到计算机时直接指定端口号,例如

http://myjavadomain.com:8080

This is assuming that your DNS is configured so that it resolves myjavadomain.com to 172.16.1.10 already. 假设已配置您的DNS,以便将myjavadomain.com解析为172.16.1.10。

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

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