20

I am trying to develop a client server TCP/IP application. I have a server running on my PC and the client app is running on the mobile. They both are in the same Wi-Fi network, but I cannot establish a connection between them. While debugging the client Android app showed the following error:

09-21 01:08:40.422: W/System.err(8536): java.net.ConnectException: failed to connect to /192.168.15.115 (port 4449): connect failed: EHOSTUNREACH (No route to host)
09-21 01:08:40.453: W/System.err(8536):     at libcore.io.IoBridge.connect(IoBridge.java:114)
09-21 01:08:40.453: W/System.err(8536):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
09-21 01:08:40.453: W/System.err(8536):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
09-21 01:08:40.453: W/System.err(8536):     at java.net.Socket.startupSocket(Socket.java:566)
09-21 01:08:40.453: W/System.err(8536):     at java.net.Socket.tryAllAddresses(Socket.java:127)
09-21 01:08:40.453: W/System.err(8536):     at java.net.Socket.<init>(Socket.java:177)
09-21 01:08:40.453: W/System.err(8536):     at java.net.Socket.<init>(Socket.java:149)
09-21 01:08:40.453: W/System.err(8536):     at sabarish.example.client_mobile.MainActivity$1.onClick(MainActivity.java:61)
09-21 01:08:40.453: W/System.err(8536):     at android.view.View.performClick(View.java:3511)
09-21 01:08:40.453: W/System.err(8536):     at android.view.View$PerformClick.run(View.java:14105)
09-21 01:08:40.453: W/System.err(8536):     at android.os.Handler.handleCallback(Handler.java:605)
09-21 01:08:40.453: W/System.err(8536):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-21 01:08:40.453: W/System.err(8536):     at android.os.Looper.loop(Looper.java:137)
09-21 01:08:40.453: W/System.err(8536):     at android.app.ActivityThread.main(ActivityThread.java:4424)
09-21 01:08:40.453: W/System.err(8536):     at java.lang.reflect.Method.invokeNative(Native Method)
09-21 01:08:40.453: W/System.err(8536):     at java.lang.reflect.Method.invoke(Method.java:511)
09-21 01:08:40.453: W/System.err(8536):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-21 01:08:40.453: W/System.err(8536):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-21 01:08:40.453: W/System.err(8536):     at dalvik.system.NativeStart.main(Native Method)
09-21 01:08:40.457: W/System.err(8536): Caused by: libcore.io.ErrnoException: connect failed: EHOSTUNREACH (No route to host)
09-21 01:08:40.457: W/System.err(8536):     at libcore.io.Posix.connect(Native Method)
09-21 01:08:40.457: W/System.err(8536):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
09-21 01:08:40.457: W/System.err(8536):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
09-21 01:08:40.457: W/System.err(8536):     at libcore.io.IoBridge.connect(IoBridge.java:112)
09-21 01:08:40.457: W/System.err(8536):     ... 18 more

The code I am using:

public class MainActivity extends Activity {
    private Socket client;
    private PrintWriter printwriter;
    private EditText textField;
    private Button button;
    private String messsage;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textField = (EditText) findViewById(R.id.editText1); //reference to the text field
        button = (Button) findViewById(R.id.button1); //reference to the send button

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                messsage = textField.getText().toString(); //get the text message on the text field
                textField.setText("");      //Reset the text field to blank

                try {
                    client = new Socket("192.168.15.115", 4449);  //connect to server
                    printwriter = new PrintWriter(client.getOutputStream(),true);
                    printwriter.write(messsage);  //write the message to output stream

                    printwriter.flush();
                    printwriter.close();
                    client.close();   //closing the connection

                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }); 
    }
}

What am I doing wrong?

Manoj Sharma
  • 1,467
  • 2
  • 13
  • 20
sash
  • 1,124
  • 2
  • 15
  • 32
  • 1
    unblock your firewall on your pc side, chances are the port is blocked or ... confirm and check that your private ip address is in the 192.168.x.y range and the dhcp on the wifi is indeed dishing out the same network address range of 192.168.x.y to your android device... – t0mm13b Sep 20 '12 at 23:44

7 Answers7

17

The no route error indicates that when the TCP connection was attempted, the connection failed because the underlying protocol software could not find a network node to network node route to the designated target node.

The fix is somewhat operating system dependent but it mainly requires you to set up your routing tables so that the device from which you are trying to connect is able to figure out how to reach the device to which you want to connect.

Normally you specify a gateway to which when you attempt to connect, the connection request goes out the network gateway to be resolved by other information technology devices such as routers, etc.

This looks to be an Android device, so the first thing is to make sure that you have connectivity whether WiFi or cell. Another would be to make sure that the WiFi or cell is turned on and operational.

The IP address you specify is normally within a private sub-net. So thing to check is is whether the device that you are trying to connect to is on the same sub-net that your device is on.

Here is a document on sub-nets and routing.

Quote from comment on Android by user destenson

A comment from user destenson on May-13-2017 adds this additional information specific to Android and the error messages provided in the posting.

Since this is android the first thing you need to look at is permissions. 09-21 01:08:40.457: W/System.err(8536): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85) indicates that the connection was denied by policy on the device.

Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
  • 1
    Since this is android the first thing you need to look at is permissions. 09-21 01:08:40.457: W/System.err(8536): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85) indicates that the connection was denied by policy on the device. – Dennis Estenson May 13 '17 at 00:02
6

This looks like a networking problem not a Java problem. Either:

  • the networking on the client either doesn't know how to route packets to the server,
  • the client's connection attempt on port 4449 is being blocked by a firewall or packet filter, or
  • you are using the wrong IP address.

(The "No route to host" message suggests that it is the first problem, but firewalls are sometimes configured to give misleading responses to traffic that is unwanted.)

Anyway, you would be better off looking for a site on how to configure and / or networks and routing.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

Hello I had the same problem in my mac and running the app in android device. I had to do following 2 things to make it work:

  1. Switch firewall off in the mac
  2. Enable the infrared receiver (System Pref > Security > Firewall > Advanced)

And it worked!

trial999
  • 1,646
  • 6
  • 21
  • 36
1

I face the same problem when I developed an android application communicate with java desktop server, to solve the problem just disconnect the usb cable that connect the android phone with the PC.

1

Turn off mobile WiFi and then turn it on

WifiManager mWifiManager = (WifiManager) mContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

mWifiManager.setWifiEnabled(false);
mWifiManager.setWifiEnabled(true);

and try again to create socket connection

Ali Moghadam
  • 1,026
  • 1
  • 9
  • 8
0

download a ping app on your phone and try to ping the server ip if it is ok ... if using apache put the local machine IP in the httpd.conf configuration file.

# Change this to Listen on specific IP addresses as shown below to 
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 127.0.0.1:80
Listen 192.168.1.30:80  (your IP)
-1

Yep..

The host is unreachable, just like that. However, if everything is oK (well configured node/network, good wifi signal,..) You can force the process using ping serverIP (from phone) or ping smartphone (from server).

The ping (ICMP packets) will force the router to speedup the new routing (way to reach the host)... send lot of pings (-n option) eg: ping google.com -n 1000 (1000 consecutive pings)

Jose Almeida
  • 54
  • 1
  • 6
  • Do the ping at the same time you try to connect (app)... You might consider to do a telnet to the serverPort (to make sure its open) – Jose Almeida Oct 19 '13 at 02:15
  • A `ping` may populate the local ARP table but it's unlikely to affect anything else, including routing tables (which are static or configured with a protocol like BGP, not by devices). And since it only takes a few ms to do an ARP to get the MAC address for an IP address on the same local subnet, you're not going to speed anything up. – Brian White Nov 27 '14 at 16:42