How do i go about making my Java app run an HTTP server on some socket (e.g 172.16.1.10:8080) and make it so that when another computer on the network connects to a domain (e.g http://myjavadomain.com) it gets redirected to the socket?
How do i go about making my Java app run an HTTP server on
Share
If you want to run a fully fledged HTTP server then you will probably want to use some external library. For instance, Tomcat is written in Java, but there is also SUN’s httpserver package. If it’s just a simple socket server you’re after, you can use the built-in classes from the java.net package:
This will listen for incoming socket connections on port 8080 and create a new
Socketwhenever a client connects. You can then communicate with the client through theSocket‘sInputStreamandOuputStream, which you would probably do in a separate Thread, so that yourServerSocketcan continue listening for incoming connections from other clients.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. One possible solution using iptables is given on this website:
But the easiest solution would be to just specify the port number directly when connecting to your machine, e.g.
This is assuming that your DNS is configured so that it resolves myjavadomain.com to 172.16.1.10 already.