no route to host
Asked Answered
S

2

8

I wrote an app on my phone with Android. I'm debugging it on phone. I try to connect to a server written in C#, but every time I try to connect, I have exception:

EHOSTUNREACH (No route to host)

I add to manifest:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

My code to connect:

try {
        InetAddress serverAddr = InetAddress.getByName(IP);
        int PortI = Integer.parseInt(Port);
        socket = new Socket(serverAddr, PortI);
}
Scag answered 28/1, 2013 at 16:37 Comment(4)
my server is in lan on my pc adress 192.168.0.102Scag
your android is an emulator ?Vasques
no I debug on device (Galaxy Ace)Scag
192.168.*.* is a private LAN address. You can't reach it from outside the same LAN.Cultivator
H
4

Try this!

  1. Check your device is connected with internet which make request to server and on same network when testing in LAN network.

  2. Check you have disabled the firewall of server if you are connected in LAN.

  3. Check the URL where you are sending request.

Heinie answered 28/1, 2013 at 16:39 Comment(12)
My device is connected with internet. I use WifiManager wifimanagerr = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE); if(wifimanagerr.isWifiEnabled()) { MyAddress = Formatter.formatIpAddress(wifimanagerr.getConnectionInfo().getIpAddress()); this.showContext(MyAddress); }else { showContext("Włącz WIFI"); } And I show my Adress IP like 192.168.0.104Scag
@user1678400 Check whether its connecting in browser or not.Heinie
I can surf on internet using browser.Scag
Not internet open browser and open the url of web service of server then check it. Its running or not.Heinie
what services? I wrote server i C# as windows applicationScag
What is ipaddress of your server?? If ipaddress is 192.168.0.102 then open browser then type this 192.168.0.102 then check its connecting or not.Heinie
it's not connect. I use 192.168.0.102 and 192.168.0.102:8080 but it's not workingScag
This is the reason you are getting the no route to host error because 192.168.0.102 not allows to connect to you. To solve this make ensure you are on same network and disabled the firewall of 192.168.0.102 this pc from windows control panel.Heinie
I have disabled Firewall (Windows 8 Firewall) on my Pc. And I have disabled antivirusScag
Ok it's working. I change my router to older. New device blocked something.Scag
@user1678400 please accept this answer if its solved your problem.Heinie
@AjayS That did the trick for me, thanks a bunch!Potemkin
C
2

Just Restart Android Studio and do the following

[File]-->[Invalidate Caches/Restart] --> choose [Invalidate and Restart]

This will probably fix the problem. Well, It did for me.

Crookes answered 8/6, 2021 at 5:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.