gpt4 book ai didi

Android套接字客户端超时

转载 作者:行者123 更新时间:2023-11-29 14:08:01 25 4
gpt4 key购买 nike

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

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

该应用程序在 android 7"平板电脑,Android 2.1 上运行

以下是我认为最相关的代码 fragment 。我认为重要的是要提一下,无论有没有 Wi-Fi 连接,都会发生这种情况。我确信我不需要 list 中显示的所有权限,但我根据我阅读的其中一个线程的建议包含了它们。

抛出的异常是:“java.net.SocketTimeoutException:套接字未连接”。我试过在没有运气的情况下增加超时限制

这是尝试连接到外部服务器的 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();
}
}

我的 list 文件如下所示:

<?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 套接字服务器:

$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

最佳答案

首先检查你的网络连接类型,如果你使用的是wifi,问题可能是你设置的wifi channel 引起的,更改它并重试。

祝你好运。

关于Android套接字客户端超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5586206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com