Android套接字客户端超时

时间:2011-04-07 19:03:32

标签: android sockets timeout client

我希望有人可以为我的问题做出贡献。我花了很多时间寻找解决方案,包括这个论坛,但还没有成功。

我有一个php套接字服务器正在运行,我正在尝试让我的android应用程序连接到它并交换数据。我知道php服务器工作,至少对于一个php客户端,所以我猜这个问题是在android客户端的某个地方。

该应用程序在Android 7“平板电脑,Android 2.1

上运行

以下是我认为最相关的代码片段。我认为重要的是要提到这种情况发生在有Wi-Fi连接而不是。我确定我不需要清单中显示的所有权限,但我将其包含在我读过的其中一个主题的建议中。

抛出的异常说:“java.net.SocketTimeoutException:Socket未连接”。我试过没有运气就增加了超时限制

这是尝试连接外部服务器的Android客户端:

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();
    }
}

我的清单文件如下:

<?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>

php socket服务器:

$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);

我已经尝试用

查看我的InetSocketAddress是否有效
InetSocketAddress p1 = (InetSocketAddress)remoteAdr;
Log.d(TAG, "isUnresolved: " + p1.isUnresolved());

使用正确的IP地址和端口号,返回false。

然后为了比较,我尝试了一个不存在的不同IP地址。这在isUnresolved()检查中返回true。 port.connect()抛出一个UnknownHostException,并显示消息“Host is unresolved”。

从这个测试中我猜我的InetSocketAddress是有效的。

有没有人知道为什么我的客户会超时?

编辑: 我在HTC Desire上尝试了我的代码(见上文)并且日志显示了相同的信息,因此看起来好像这个问题没有修复到我的平板电脑上。我在代码中遗漏了一些东西,无论是在客户端还是服务器端?

我想知道服务器是否拒绝连接,这就是客户端超时的原因。我的意思是我的InetSocketAddress是有效的所以我必须做正确的事。

我包含使用“adb logcat t:D AndroidRuntime:E”生成的日志。有没有人在阅读此日志后有线索

----------------------------------------------------------------------------------
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

1 个答案:

答案 0 :(得分:0)

首先检查您的网络连接类型,如果您使用的是wifi,问题可能是由您设置的wifi频道引起的,更改它&amp;再试一次。

祝你好运。