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.