Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Isma3ilMohamed opened this issue Jul 19, 2019 · 7 comments
Closed

Comments

@Isma3ilMohamed
Copy link

Isma3ilMohamed commented Jul 19, 2019

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
@Isma3ilMohamed Isma3ilMohamed changed the title How to make remove server Caused by: java.net.BindException: Cannot assign requested address Jul 19, 2019
@stefan-reich
Copy link

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

@Isma3ilMohamed
Copy link
Author

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
Copy link

I don't think you can, not directly.

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

@ItsHarper
Copy link

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
Copy link

stefan-reich commented Aug 2, 2019

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

Here's some docs for that.

@Isma3ilMohamed
Copy link
Author

@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
Copy link

I'll contact you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants