NanoHttpd / nanohttpd

Tiny, easily embeddable HTTP server in Java.
http://nanohttpd.org
BSD 3-Clause "New" or "Revised" License
6.94k stars 1.69k forks source link

Not able to access without port number(:8080) #612

Closed kishansinghpanwar closed 3 years ago

kishansinghpanwar commented 3 years ago

Hi, I have searched everywhere but can't figure out how to do this. Basically, I want to remove the :8080 from my URLS so my portal will be ready for deployment. When I type in http://191.161.43.159/ I want to access the portal, rather than typing in http://191.161.43.159:8080

How does one do this?

Any advice would be great!

Thanks in advance, Kishan Singh

NoahAndrews commented 3 years ago

To do this, pass in 80 as the parameter to the NanoHTTPD constructor, rather than 8080. Port 80 is the port that browsers default to for HTTP (not HTTPS).

kishansinghpanwar commented 3 years ago

@NoahAndrews Thanks for the suggestion, but whenever I'm trying to provide the port number different than 8080, I'm getting errors while starting the server connection and same result I got when I tested with "80" port.

Getting error while binding InetSocketAddress to ServerSocket, here are the code:

try {
    myServerSocket.bind(hostname != null ? new InetSocketAddress(hostname, myPort) : new InetSocketAddress(myPort));
    hasBinded = true;
} catch (IOException e) {
    this.bindException = e;
    return;
}

And here are the error logs :

W/System.err: java.net.BindException: bind failed: EACCES (Permission denied)
W/System.err:     at libcore.io.IoBridge.bind(IoBridge.java:103)
W/System.err:     at java.net.PlainSocketImpl.socketBind(PlainSocketImpl.java:157)
W/System.err:     at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:427)
W/System.err:     at java.net.ServerSocket.bind(ServerSocket.java:377)
W/System.err:     at java.net.ServerSocket.bind(ServerSocket.java:331)
W/System.err:     at java.lang.Thread.run(Thread.java:764)
W/System.err: Caused by: android.system.ErrnoException: bind failed: EACCES (Permission denied)
W/System.err:     at libcore.io.Linux.bind(Native Method)
W/System.err:     at libcore.io.ForwardingOs.bind(ForwardingOs.java:60)
W/System.err:     at libcore.io.IoBridge.bind(IoBridge.java:99)
W/System.err:   ... 6 more

I have tried to find the solution for this error and found this error appears when the app don't have Internet permission(android.permission.INTERNET) or access network state permission(android.permission.ACCESS_NETWORK_STATE), but I have already defined both permissions in the AndroidManifest file.

NoahAndrews commented 3 years ago

Oh, binding to ports below 1024 typically requires root permissions on Linux, so you'd have to root your phone for that to work on Android. If you're looking to distribute the app widely, you're going to be stuck with 8080.

I actually work for a company that sells an Android device that you access over port 8080, and I don't think we really get any complaints about having to specify the port.

kishansinghpanwar commented 3 years ago

Thanks for your help. Yes, I also think so this might be a reason, but I think instead of rooting my phone, I can bind the app with the framework as System App, and I think in that case the app have access of 80 port.