2

I am new to Android programming and I have been facing problems that didn't exist in native Java. When I run this code in my computer, it runs correctly. But, when I run it in my device.I get nothing, I even tried to post the message to UI and there's no logcat for this. I am not sure what is wrong.

                try{
                    Socket socket = new Socket(serverAddr, SERVER_PORT);
                    Log.i("TAG","Socket connected");    
                }catch(IOException e){
                    Log.i("TAG","Socket not connected");
                }

Update 1: I just changed the code..nothing much and realized that after 2 minutes or so it does what it was supposed to do?? Is is anything to do with keep alive flags? Or is there anyway that I can run the code just for a second or two and stop it. Please understand that the code below the socket creation line executes only after 2 minutes if the server is dead. Here below is my code:

    try{
InetAddress serverAddr = InetAddress.getByName(serverIP);   
//Line below executes with no delay
postToUI("Trying to connect to standalone server" + "\n\n");
socket = new Socket(serverAddr, SERVER_PORT);
//Line below executes after 2 minutes
postToUI("Successfully connected to standalone server" + "\n\n");

}catch(ConnectException e){
    postToUI("Socket not connected");
}catch(IOException e){
    postToUI("Socket not connected");
}   
Milan
  • 1,845
  • 5
  • 19
  • 33
  • I am not sure but you can try this: http://stackoverflow.com/questions/1443166/android-how-to-check-if-the-server-is-available – Dmytro Danylyk Feb 04 '12 at 10:16
  • It talks about alive host. In which case even if the server application is not listening but, if server host is alive then it will be good. In my case i want to find out if my server is accepting connection. – Milan Feb 04 '12 at 10:19
  • It looks like some error is thrown. Try looking for it in the logs or replace `IOException` with `Throwable`. – Piotr Praszmo Feb 04 '12 at 12:24
  • If you are getting neither log message in LogCat, then you are not executing the code you have shown above. – CommonsWare Feb 04 '12 at 12:35
  • @CommonsWare the code just before the try block is executing. – Milan Feb 04 '12 at 12:51
  • Then one of those log messages will be in LogCat, or you will get a "force close" dialog due to an unhandled exception and that stack trace will be in LogCat. Particularly on the one in the `catch` block, please add the exception as a third parameter to the `i()` call, so you see the stack trace. – CommonsWare Feb 04 '12 at 13:18
  • Like i said..i got exception just after couple of minutes...i am not sure...what is wrong – Milan Feb 04 '12 at 20:53

1 Answers1

0

I have done very few Andrioid development so don't bite me smiley.

Possible reasons why the host might not communicate with the client

  • Host has firewall which might be closing the connection (unlikely)
  • Host might have unexpectly shutdown e.g power failure (unlikely)
  • Host might not properly port forwared (possibly)
  • Your andriod app doesn't have the proper manifest that allows the use of sockets.
  • Your andrioid device you are testing on might have its internet disabled which can enabled easily.
  • Host's address could have changed if it is dynamic.

For logcat, I think that logcat only displays log messages for the main thread. But I am unsure.

Daniel Lopez
  • 1,728
  • 3
  • 24
  • 40