skari
skari

Reputation: 55

Android socket client times out

I hope someone can contribute to a solution for my problem. I've spent a lot of my time searching for a solution, including this forum, but haven't been successful.

I have a php socket server running and I'm trying to have my android application connect to it and exchange data. I know that the php server works, at least for a php client so I'm guessing the problem is somewhere within the android client.

The application is run on android 7" tablet, Android 2.1

Below are, what I think, the most relevant code snippets. I think it's important to mention that this occurs both when there is wi-fi connection and not. I'm sure I don't need all of the permissions shown in the manifest but I included them at the suggestion of one of the threads I read.

The exception that is thrown says: "java.net.SocketTimeoutException: Socket is not connected". I've tried increasing the timeout limit without luck

This is the android client that tries to connect to an external server:

import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class test extends Activity{
    private static final String TAG = "socketTest";

    public SocketAddress remoteAdr = new InetSocketAddress("111.222.333.444", 1234);
    public Socket socket;

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

        Log.d(TAG, "Start the socket thread..");
        startThread();
    }

    void startThread(){
        Thread t = new Thread(){
            public void run(){
                Log.d(TAG, "Socket thread started !");
                boolean loop = true;

                while(loop){
                    try{
                        Log.d(TAG, "Create the socket");
                        socket = new Socket();

                        Log.d(TAG, "Try to connect the socket");
                        socket.connect(remoteAdr, 15000);

                        Log.d(TAG, "I'm never shown..");
                    }catch(Exception e){
                        Log.e(TAG, "Error when connecting: " + e);
                    }

                        Log.d(TAG, "Done trying");
                        loop = false;
                    }
                }
            };
        t.start();
    }
}

My manifest file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.henda.test"
     android:versionCode="1"
     android:versionName="1.0">

    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_CHECKIN_PROPERTIES"></uses-permission>
    <uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>
    <uses-permission android:name="android.permission.DUMP"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_OWNER_DATA"></uses-permission>
    <uses-permission android:name="android.permission.READ_INPUT_STATE"></uses-permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"></uses-permission>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name="com.socket.test.test"
                 android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

The php socket server:

$loop = 1;
set_time_limit (1);

// set some variables
$host="111.222.333.444";
$port = 1234;

// Create the socket, bind it to a port and start listening (accepts 3 connectinons in the queue).
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Could not create socket \n");
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket \n");
$result = socket_listen($socket, 3) or die("Could not set up socket listener \n");

do{    
    // spawn another socket to handle communication and
    $spawn = socket_accept($socket) or die("Could not accept incoming connection \n");

    // Accept incoming connections and wait for user input
    $input = socket_read($spawn, 1024) or die("Could not read input \n");

    switch(trim($input)){
        case "someCase":{
            // do something here, f.x. connect to a database
            // code omitted
            break;
        }
        case "END":
        {
            $loop = 0;
            break;
        }
    }while($loop);
socket_close($socket);

I've already tried to see if my InetSocketAddress is valid with

InetSocketAddress p1 = (InetSocketAddress)remoteAdr;
Log.d(TAG, "isUnresolved: " + p1.isUnresolved());

Using my correct IP-address and port number this returns false.

Then for comparison I tried a different IP address which does not exist. This returned true on the isUnresolved() check. The port.connect() threw an UnknownHostException with the message "Host is unresolved".

From this test I guess my InetSocketAddress is valid.

Does anyone have a clue why my client gets timed out ??

EDIT: I tried my code (see above) on a HTC Desire and the log showed the same information so it appears as if this problem is not fixed to my tablet. Am I missing something in my code, either on the client side or the server side ?

I wonder if the server is refusing the connection and that's why the client times out. I mean my InetSocketAddress is valid so I must be doing something right.

I include the log generated with "adb logcat t:D AndroidRuntime:E". Does anyone have a clue after reading this log

----------------------------------------------------------------------------------
D/t       ( 1132): --------------------------------------------
D/t       ( 1132): Start the socket thread..
D/t       ( 1132): Socket thread started !
D/t       ( 1132): Create the socket
D/t       ( 1132): Try to connect the socket
W/InputManagerService(  738): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@46fe5eb0 (uid=10001 pid=831)
I/startInputInner(  831):  mService.startInput fail,retry left=2
W/InputManagerService(  738): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@46fe5eb0 (uid=10001 pid=831)
I/startInputInner(  831):  mService.startInput fail,retry left=1
W/InputManagerService(  738): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@46fe5eb0 (uid=10001 pid=831)
I/startInputInner(  831):  mService.startInput fail,retry left=0
W/InputManagerService(  738): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@46fe5eb0 (uid=10001 pid=831)
I/ActivityManager(  738): Displayed activity com.henda.test/com.socket.test.test: 350 ms (total 350 ms)
I/startInputInner( 1132):  mService.startInput fail,retry left=2
I/startInputInner( 1132):  mService.startInput fail,retry left=1
I/startInputInner( 1132):  mService.startInput fail,retry left=0
E/libagl  (  738): copybit failed (Unknown error: -1)
D/WPA_CTRL_IFACE( 2064): Global Control: cmd=[DRIVER RSSI-APPROX] reply=[WIFI RSSI -54
D/WPA_CTRL_IFACE( 2064): ]
D/WPA_CTRL_IFACE( 2064): Global Control: cmd=[DRIVER LINKSPEED] reply=[LINKSPEED 54
D/WPA_CTRL_IFACE( 2064): ]
D/dalvikvm(  738): GC freed 1990 objects / 104424 bytes in 169ms
D/WPA_CTRL_IFACE( 2064): Global Control: cmd=[DRIVER RSSI-APPROX] reply=[WIFI RSSI -55
D/WPA_CTRL_IFACE( 2064): ]
D/WPA_CTRL_IFACE( 2064): Global Control: cmd=[DRIVER LINKSPEED] reply=[LINKSPEED 48
D/WPA_CTRL_IFACE( 2064): ]
D/dalvikvm( 1529): GC freed 261 objects / 14536 bytes in 80ms
D/dalvikvm(11058): GC freed 8233 objects / 526624 bytes in 87ms
D/WPA_CTRL_IFACE( 2064): Global Control: cmd=[DRIVER RSSI-APPROX] reply=[WIFI RSSI -57
D/WPA_CTRL_IFACE( 2064): ]
D/WPA_CTRL_IFACE( 2064): Global Control: cmd=[DRIVER LINKSPEED] reply=[LINKSPEED 48
D/WPA_CTRL_IFACE( 2064): ]
D/dalvikvm(  976): GC freed 215 objects / 17880 bytes in 94ms
D/dalvikvm(11058): GC freed 8566 objects / 519976 bytes in 83ms
D/WPA_CTRL_IFACE( 2064): Global Control: cmd=[DRIVER RSSI-APPROX] reply=[WIFI RSSI -53
D/WPA_CTRL_IFACE( 2064): ]
D/WPA_CTRL_IFACE( 2064): Global Control: cmd=[DRIVER LINKSPEED] reply=[LINKSPEED 48
D/WPA_CTRL_IFACE( 2064): ]
D/WPA_CTRL_IFACE( 2064): Global Control: cmd=[DRIVER RSSI-APPROX] reply=[WIFI RSSI -53
D/WPA_CTRL_IFACE( 2064): ]
D/WPA_CTRL_IFACE( 2064): Global Control: cmd=[DRIVER LINKSPEED] reply=[LINKSPEED  1
D/WPA_CTRL_IFACE( 2064): ]
D/dalvikvm( 1523): GC freed 43 objects / 2064 bytes in 73ms
D/dalvikvm(11058): GC freed 8232 objects / 526504 bytes in 87ms
D/WPA_CTRL_IFACE( 2064): Global Control: cmd=[DRIVER RSSI-APPROX] reply=[WIFI RSSI -52
D/WPA_CTRL_IFACE( 2064): ]
D/WPA_CTRL_IFACE( 2064): Global Control: cmd=[DRIVER LINKSPEED] reply=[LINKSPEED 54
D/WPA_CTRL_IFACE( 2064): ]
D/WPA_CTRL_IFACE( 2064): Global Control: cmd=[DRIVER RSSI-APPROX] reply=[WIFI RSSI -54
D/WPA_CTRL_IFACE( 2064): ]
D/WPA_CTRL_IFACE( 2064): Global Control: cmd=[DRIVER LINKSPEED] reply=[LINKSPEED 48
D/WPA_CTRL_IFACE( 2064): ]
D/dalvikvm( 2199): GC freed 499 objects / 24120 bytes in 95ms
D/dalvikvm(11058): GC freed 8566 objects / 519944 bytes in 84ms
D/WPA_CTRL_IFACE( 2064): Global Control: cmd=[DRIVER RSSI-APPROX] reply=[WIFI RSSI -55
D/WPA_CTRL_IFACE( 2064): ]
D/WPA_CTRL_IFACE( 2064): Global Control: cmd=[DRIVER LINKSPEED] reply=[LINKSPEED 48
D/WPA_CTRL_IFACE( 2064): ]
D/dalvikvm(  831): GC freed 1006 objects / 55656 bytes in 107ms
D/WPA_CTRL_IFACE( 2064): Global Control: cmd=[DRIVER RSSI-APPROX] reply=[WIFI RSSI -54
D/WPA_CTRL_IFACE( 2064): ]
D/WPA_CTRL_IFACE( 2064): Global Control: cmd=[DRIVER LINKSPEED] reply=[LINKSPEED 48
D/WPA_CTRL_IFACE( 2064): ]
D/dalvikvm(11058): GC freed 8231 objects / 526584 bytes in 83ms
D/WPA_CTRL_IFACE( 2064): Global Control: cmd=[DRIVER RSSI-APPROX] reply=[WIFI RSSI -53
D/WPA_CTRL_IFACE( 2064): ]
D/WPA_CTRL_IFACE( 2064): Global Control: cmd=[DRIVER LINKSPEED] reply=[LINKSPEED 36
D/WPA_CTRL_IFACE( 2064): ]
E/t       ( 1132): Error when connecting: java.net.SocketTimeoutException: Socket is not connected
D/t       ( 1132): Done trying

Upvotes: 2

Views: 4032

Answers (1)

caval
caval

Reputation: 11

Firstly check your type of network connection, if you are using wifi, the issue may caused by your setting of wifi's channel, change it & try again.

good luck.

Upvotes: 1

Related Questions