Android套接字客户端超时
我希望有人能为我的问题的解决方案做出贡献。我花了很多时间寻找解决方案,包括这个论坛,但没有成功。
我有一个正在运行的 php 套接字服务器,我试图让我的 android 应用程序连接到它并交换数据。我知道 php 服务器可以工作,至少对于 php 客户端来说是这样,所以我猜测问题出在 android 客户端内的某个地方。
该应用程序在 android 7" 平板电脑(Android 2.1)上运行
。我认为,下面是最相关的代码片段。我认为值得一提的是,在有 wi-fi 连接和没有 wi-fi 连接时都会发生这种情况。我确信我不需要清单中显示的所有权限,但我根据我读到的线程之一的建议将它们包含在内。
引发的异常是:“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();
}
}
我的清单文件如下所示:
<?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 并显示消息“主机未解析”。 从这个
测试我猜我的 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先检查您的网络连接类型,如果您使用的是wifi,则问题可能是您的wifi频道设置引起的,请更改它并重新设置。再试一次。
祝你好运。
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.