NanoHttpd / nanohttpd

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

Caused by: java.net.BindException: Cannot assign requested address #551

Closed Isma3ilMohamed closed 4 years ago

Isma3ilMohamed commented 5 years ago

hey everyone i want to create remove server on my phone to can access it from anywhere so when i try local host everything work well but when try to connect to my public ip this error happened

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1453) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1565) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1453)  Caused by: java.net.BindException: Cannot assign requested address at java.net.PlainSocketImpl.socketBind(Native Method) at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:368) at java.net.ServerSocket.bind(ServerSocket.java:377) at java.net.ServerSocket.bind(ServerSocket.java:331) at fi.iki.elonen.NanoHTTPD$ServerRunnable.run(NanoHTTPD.java:1761) at java.lang.Thread.run(Thread.java:762)

and that is my Server Class

class AndroidWebServer : NanoHTTPD{
    constructor(  port:Int):super(port){
    }
    constructor( hostName:String, port: Int) : super(hostName,port){

    }
    override fun serve(session: IHTTPSession): Response? {

        val uri = session.uri

        if (uri == "/hello") {
            val response = """
                <html>
  <head>
    <title>Android Server</title>
  </head>
  <body>
    <h1>
      Hello Welcome to android server
    </h1>
    <p>
      here is basic test of android server
    </p>
  </body>
</html>
            """.trimIndent()
            return newFixedLengthResponse(response)
        }
        return null
    }

}

and that is Main Activity


class MainActivity : AppCompatActivity() {
   override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        getPublicIP()
        val server=AndroidWebServer("156.201.179.45",8080)

        serverController(server)

    }

    private fun serverController(server: AndroidWebServer) {
        btnStop.isEnabled = false
        buttonStart.setOnClickListener {

            server.start()
            initIPAddress()

            buttonStart.isEnabled = false
            btnStop.isEnabled = true
        }
        btnStop.setOnClickListener {
            server.stop()

            buttonStart.isEnabled = true
            btnStop.isEnabled = false
        }
    }

    @SuppressLint("SetTextI18n")
    private fun initIPAddress() {
        val wm = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
        val ip = Formatter.formatIpAddress(wm.connectionInfo.ipAddress)
        txtIpAddress.text = "Server running at: $ip: "
        Log.i("TAG", "onCreate: $ip")
    }

    fun getPublicIP() {
        thread {
            val doc = Jsoup.connect("http://www.checkip.org").get()
            doc.getElementById("yourip").select("h1").first().select("span").text()
            Log.i("TAG",doc.text())
        }

    }
}

```so i can't understand this error
and need help to solve it or to achieve 
and thanks for all
stefan-reich commented 4 years ago

Your phone is firewalled and doesn't have a public IP.

Isma3ilMohamed commented 4 years ago

Hey @stefan-reich thanks for reply but how to get public ip to phone and pass firewalled how can i achieve my purpose

stefan-reich commented 4 years ago

I don't think you can, not directly.

You can use services like ngrok.io to serve web pages from your phone.

NoahAndrews commented 4 years ago

You can't do it directly. You would have to have a separate server in between to proxy the traffic to your phone.

stefan-reich commented 4 years ago

Oh wait, I think ngrok has no app. http://serveo.net/ seems to have an Android app.

Here's some docs for that.

Isma3ilMohamed commented 4 years ago

@stefan-reich @NoahAndrews thanks for your replies but about production app i still need to depend on Termux or BusyBox Are there other ways to do that?

stefan-reich commented 4 years ago

I'll contact you