7
votes

I have written an app to start my server at home remotely. The app works without problems in the emulator and also on my smartphone (HTC desire, Android 2.2) when WiFi is enabled. However it does not work when WiFi is disabled.

Before restarting I first check if it's already running. To do this I use sockets and I first connect to a dyndns address. After that I try to connect to my ip-box where I can switch on my computer by sending commands via a socket.

When the connection to that socket fails I know the server is not running.

The relevant code is:

        socket = new Socket();
        socket.connect(new InetSocketAddress(serverName, port), 10000);
            status = socket.isConnected() == true;
        socket.close();

If there's an exception (SocketException) I know that the server is not running. This approach works perfectly when I have switched WiFi on. However if WiFi's not switched on then the connect always says it's ok, even if it could not establish a connection since the server is not available.

Is there a way to check if the the connection is really established, even if WiFi is disabled?

Any suggestions welcome!

Thanks,

Rudi

2
How do you know that the connection really failed? Even if the WiFi is disabled, a connection may still happen through the cellular data network. See this to make sure that the cellular data network is disabled: newbiehelper.blogspot.com/2011/07/… - Theo
Since the server I am trying to boot is not running it is impossible to establish a connection. When I run the program when wifi is enabled then the call to socket.connect() fails as excepted. I should add that I just found a very similar question on stackoverflow, unfortunately unanswered. Basically the other question refers to problems in 3G-mode, so obviously I am not alone to run into this problem. - Rudolf Ziegaus
Ok, so it works with Wifi on, it doesn't work with Wifi off, and this happens on both the emulator and on the real phone? - Theo
This only happens on the real phone, on the emulator I can't switch off Wifi - just tried, since I have never bothered before to switch it off in the emulator. Actually I always assumed sockets should behave the same when run in 3G-mode and in Wifi mode - was a bit naive;-) - Rudolf Ziegaus
Your issue seems to be similar to the one in the following thread. Perhaps there's some port filtering on your cell network connection? Try the port 80 suggestion. stackoverflow.com/questions/5749612/… - Theo

2 Answers

2
votes

Try to open your socket like this :

public boolean connect(String ip, int port) {
    try {
        this.clientSocket = new Socket(ip, port);
        clientSocket.setSoTimeout(timeout);
        this.outToServer = new DataOutputStream(clientSocket
                .getOutputStream());
        this.inFromServer = clientSocket.getInputStream();
        isconnected = true;
    } catch (IOException e) {
        Log.e("TCPclient", "connection failed on " + ip + ":" + port);
        isconnected = false;
        return isconnected;
    }
    Log.e("TCPclient", "connection to " + ip + " sucessfull");
    return isconnected;
}

If connection is not successful , it will generate an IOException (work when wifi enabled and no server , and when wifi is not enabled(HTC desire 2.3)).
This code is not really correct ,it's just a short version

EDIT Try to check wfi state like this (it is not practical but it should work)

    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      ConnectivityManager cm = (ConnectivityManager)  getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
      if (ni.isConnected()) {
        Toast.makeText(this,"Wifi enabled", Toast.LENGTH_LONG).show();  
        Log.d("WiFiStateTestActivity", "WiFi!");
      } else {
        Toast.makeText(this,"Wifi not enabled", Toast.LENGTH_LONG).show();  
        Log.d("WiFiStateTestActivity", "not WiFi!");
      }
    }
1
votes

Don't forget to set the permission in manifest.xml to allow you app to open a socket.