0
    Socket socket = null;
    PrintWriter out =null;
    InetAddress ip = InetAddress.getByName(host);
    socket = new Socket(ip,port);
    out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
    out.println(myString);
    if(socket!=null)
        socket.close();

My android code is this. I need to save the string in a file if there is any kind of problem (no internet connection, no server avaible, ecc... I tried with a try-catch, but even if there is no internet/server avaible, no exception is thrown. Can you help me?

As As
  • 2,049
  • 4
  • 17
  • 33

2 Answers2

0

Hard to believe that the code will not throw an IOException or an UnknownHostException if you try to resolve a hostname with InetAddress.getByName(host) without a network being available. Maybe you could post more of your code (with the try-catch-block).

Also I would suggest you try using a DataOutputStream to write your string to the socket:

DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataOutputStream.writeUTF(myString);

Furthermore I would suggest that you check the availability of your internet connection by either listening to the apropriate broadcasts in android or by asking the connection-manager.

public static boolean isNetworkConnectionAvailable(Context context) {

    boolean isNetworkConnectionAvailable = false;

    ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService("connectivity");
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

    if(activeNetworkInfo != null) 
    {
        isNetworkConnectionAvailable = 
                activeNetworkInfo.getState() == NetworkInfo.State.CONNECTED;
    }
    return isNetworkConnectionAvailable;
}

Or see here: broadcast-receiver-for-checking-internet-connection-in-android-app.

Community
  • 1
  • 1
mwhs
  • 5,878
  • 2
  • 28
  • 34
  • Ok, in this way i know if i'm connected, but if i'm connected but the server is not reachable, i need to save the string in a file. I saw that `InetAddress.isReachable()` is not reliable. – As As Oct 17 '13 at 13:10
  • The solution to your problem is not to rely on the fact that something has been transfered to another host just because there was no exception. I would strongly suggest that you send an acknowledgment back from the server to the client. If there is no such acknowledgement after a certain time (read timeout) then you should write the string to a file. Look up books on protocol design maybe. – mwhs Oct 17 '13 at 13:13
-1

You can use this method, InetAddress.isReachable, to check if you can ping the host. There's no direct method to check internet connectivity

usage (no try/catch)-

    if(ip.isReachable(3000)) // timeout is 3000 ms ie 3s
    {
           // internet available,do something
    } 
Ankit Rustagi
  • 5,539
  • 12
  • 39
  • 70
  • So i have just to use that method in the try-catch? I'll try thank you. – As As Oct 16 '13 at 11:26
  • I tried it, but now, even if the server is online, i can't connect to it (i'm using avd and a server in my pc, so if i remove the if the connection works correctly). So i guess if i use a server like that the ip is not reachable. – As As Oct 16 '13 at 16:45
  • Try increasing the timeout to 10000 (10 secs). – Ankit Rustagi Oct 16 '13 at 17:27
  • Still nothing. I read that `InetAddress.isReachable()` is not reliable. Are there any alternative way? – As As Oct 17 '13 at 13:11