java.net.ServerSocket.accept()不会在Android返回 [英] java.net.ServerSocket.accept () doesn't return on Android

查看:404
本文介绍了java.net.ServerSocket.accept()不会在Android返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做的方法telnet到一个无根的Droid。我有网​​络许可活动的,我有我的设备连接在同一个网络作为我的的Mac OS X 通过WiFi框,我能ping我打开的端口。

I am trying to make a way to telnet to an unrooted Droid. I have the INTERNET permission active, I have my device connected on the same network as my Mac OS X box via WiFi, and I am able to ping the port I opened.

在最初的实验中,我得到它的工作有根测试设备上,但我有插座处理器上的 UI线程,而不是一个单独的线程中运行。现在,我有一个单独的线程网络模块,我不能让的ServerSocket.accept()返回。它适用于谷歌的版本的Andr​​oid(香草味),但不是三星或索尼爱立信的。

In initial experiments, I got it to work on a rooted test device, but I had the socket handlers run on the UI Thread rather than a separate thread. Now that I have the network modules on a separate thread, I can't get ServerSocket.accept () to return. It works on Google's version of android (vanilla), but not on Samsung's or Sony-Ericsson's.

当我telnet到它,我尝试将超时,而的logcat 不会打印出任何异常或错误。

When I telnet to it, my attempt would time out, and logcat wouldn't print out any exceptions or errors.

下面是一个链接到我的code一google- code回购:的 Google- code库

Here is a link to a google-code repo of my code: Google-code Repository

我正在 ServerScoket.accept()在一个单独的线程,并运行在另一个线程流处理器也是如此。在我的设计评论(即我应该用处理程序 s或 AsyncTasks )是非常欢迎的。眼下,为了敬酒通过telnet收到的消息,我用的弯针处理程序通过上下文被收购

I am running ServerScoket.accept () on a separate thread, and run the stream processors on another thread as well. Comments on my design (i.e. I should use Handlers or AsyncTasks) are extremely welcome. Right now, in order to Toast the messages received via telnet, I use a Handler with the looper being acquired via a Context.

下面是什么,当我运行的netstat -n 在亚行外壳上非工作的设备:

The following is what I get when I run netstat -n on the adb shell on the non-working devices:

~$ adb shell netstat -n
Proto Recv-Q Send-Q Local Address          Foreign Address        State
tcp        0      0 127.0.0.1:7777         0.0.0.0:*              LISTEN
tcp        0      0 127.0.0.1:7203         0.0.0.0:*              LISTEN
tcp        0      0 127.0.0.1:47609        127.0.0.1:7777         ESTABLISHED
tcp        0      0 127.0.0.1:7777         127.0.0.1:47609        ESTABLISHED
tcp        0      0 127.0.0.1:47610        127.0.0.1:7777         ESTABLISHED
tcp        0      0 127.0.0.1:7777         127.0.0.1:47610        ESTABLISHED

不同的是,在工作的设备,他们列出的IP与我的端口的状态下打开,LISTEN

The difference is that in the working devices, they list an IP with my port open in the state, LISTEN.

更新:具有<使用-权限的Andr​​oid:名称=INTERNET/> 在我的 android_manifest ,我试图改变的端口号 689 。它没有工作;我有一个 BindException,使其,说我可能缺乏的INTERNET权限。所以,我把它改为 1989年,我又回到了一切工作,直到接受()。我想这是因为我跑它放在一个非root的手机,而我没有访问端口 1024 及以下。

UPDATE: Having the <uses-permission android:name="INTERNET"/> set in my android_manifest, I tried changing the port number to 689. It didn't work; I got a BindException, saying that I may be lacking the INTERNET permission. So, I changed it to 1989, and I went back to everything working until accept (). I assume this is because I ran it on a non-root phone, and I don't have access to ports 1024 and below.

更新:我跑在我的Mac一个非常类似的计划,它工作得很好,当我试图使用分配给我的IP地址远程登录到我的Mac。当我试图从另一台Mac远程登录,但它似乎没有连接它没有工作;连接会超时。它没有工作在一个ad-hoc网络,虽然。我仍然还没有使用机器人来试试吧,但我会尽快更新这个。

UPDATE: I ran a really similar program on my Mac, and it worked fine when I tried telnetting to my Mac using the IP address assigned to me. It didn't work when I tried telnetting from another Mac but it didn't seem to connect; the connection would timeout. It did work over an ad-hoc network, though. I still have yet to try it using the droid, but I will update this asap.

更新:我设法让该应用程序的工作运行香草(安卓由谷歌发布)3个独立的机器人。它的工作在一台Nexus,一首派A60(我的第一个装置,亚行已不再检测到它的某些原因。),和一个特制的,无品牌的平板电脑。不过,因为我已经提供了pretty的大奖金,我打算坚持到底看到这一点。

UPDATE: I managed to get the app working on 3 separate Droids running Vanilla (Android released by google). It worked on a Nexus, an Apanda A60 (my first device; adb has ceased to detect it for some reason.), and a custom-made, unbranded tablet. Still, because I already offered a pretty big bounty, I plan on seeing this through to the end.

如前所述,我的应用程序可与香草版本的Andr​​oid,但不能与修改后的版本。这三款手机未能运行它都是中档车型; 2 三星GT-i5503 S和A 索尼爱立信E16i的

As stated earlier, my app works with Vanilla versions of android, but not with modified versions. The three phones that failed to run it were all mid-range models; 2 Samsung GT-i5503s, and a Sony-Ericcson E16i.

推荐答案

好像你有一个网络问题,而不是一个code问题。我用你的最新的项目,它是监听的端口上,符合市场预期。

It seems like you have a networking issue, rather than a code issue. I used your latest project and it is listening on the port, as expected.

我说这 TelnetServer.setupServerSocket()来确认一些信息:

I added this to TelnetServer.setupServerSocket() to confirm some information:

Log.i("TelnetServer", "ServerSocket Address: " + this.server.getLocalSocketAddress());
try {
    Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
    while (en.hasMoreElements()) {
        NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                Log.i("TelnetServer", "Listen On: " + inetAddress.getHostAddress());
            }
        }
} catch (SocketException ex) {
    Log.e("TelnetServer", ex.toString(), ex);
}

这将打印所有的服务正在侦听的地址(如果是侦听0.0.0.0/0.0.0.0:xxx(后ServerSocket的地址印:))

This will print all of the addresses your service is listening on (if it is listening on 0.0.0.0/0.0.0.0:xxx (printed after ServerSocket Address:)).

您应该运行模拟器 -tcpdump&LT;文件&gt; 选项,并且还提供了这一点。这将确认任何正在尝试连接。我的直觉是,你的客户端不能访问服务器,这就是为什么服务器无法接收的连接 - 而不是一个问题,code

You should run emulator with the -tcpdump <file> option and also provide this. It will confirm any connection is being attempted. My hunch is that your client is not able to access the server, which is why the server is not receiving the connection - rather than an issue with the code.

请提供tcpdump的文件,你的IP地址(客户端)和logcat的输出(包括ServerSocket的地址和监听语句)作进一步的分析。

Please provide the tcpdump file, your IP Address (of the client) and the logcat output (including the ServerSocket Address and Listen On statements) for further analysis.

这篇关于java.net.ServerSocket.accept()不会在Android返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆