Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 3+中的java.nio问题;_Java_Android_Nio_Android 3.0 Honeycomb - Fatal编程技术网

Android 3+中的java.nio问题;

Android 3+中的java.nio问题;,java,android,nio,android-3.0-honeycomb,Java,Android,Nio,Android 3.0 Honeycomb,我发现并希望在我的应用程序中实现的java.nio非阻塞服务器有问题: 下面是正在发生的事情: 有时,它会起作用。它可以连接到服务器,进行简单的握手,并准备好来回发送消息。但通常情况下,它甚至没有收到连接的消息。以下是握手的工作原理: Server sends cipher for encryption Server sends "connected" to test connection and encryption Client sends username Server sends ac

我发现并希望在我的应用程序中实现的java.nio非阻塞服务器有问题:

下面是正在发生的事情: 有时,它会起作用。它可以连接到服务器,进行简单的握手,并准备好来回发送消息。但通常情况下,它甚至没有收到连接的消息。以下是握手的工作原理:

Server sends cipher for encryption
Server sends "connected" to test connection and encryption
Client sends username
Server sends acknowledgment
Client sends password
Server sends acknowledgement
Server sends login success if that's the case
Client sends acknowledgment of login
Server asks for uuid
Client provides uuid
Server says it's ready
Client says it's ready
The handshake is complete and message transfer can begin
提到的加密使用的是jasypt

每次我试着在安卓2.2、2.3和MacOSX(Java1.6)上都可以使用。在高于3.0的Android版本上,它在每个服务器生命周期(启动、运行、死亡)中工作一次。我知道Android的联网方式发生了重大变化,但我知道的唯一一点是,你不能在主线程上联网(这不是,这是一个从服务衍生出来的线程)。我已经通过
tcpdump
确认服务器实际上正在发送一条连接消息

任何帮助都会很好,奇怪的是它只在安卓3+中中断

谢谢你事先的帮助

编辑:我没有弄清楚握手失败的地方。客户端收到密码后,它失败。通过调试,我知道客户端也会收到连接的消息,因为它不是读取缓冲区上的唯一内容:

Cipher:
[0, 16, 102, 69, 106, 80, 101, 56, 77, 72, 118, 117, 77, 75, 85, 100, 57, 78, 
Connected:
0, 32, 53, 106, 43, 69, 104, 122, 87, 100, 109, 67, 85, 90, 121, 65, 83, 57, 98, 65, 78,     73, 103, 103, 66, 55, 103, 54, 55, 106, 54, 108, 54, 53, ...(Rest of buffer is zeros)...]
0,16
0,32
是消息长度(分别为16和32)。这是在TwoByteMessageLength.java中定义的


此外,这可能不是加密失败,因为这会引发一个大的ol'
EncryptionOperationNotPossibleeException
异常。

好吧,问题出在这里:

在手机接收到的阵列中,两条消息同时出现(看看问题,看看我的意思)。它只读取了全部内容(由于
InputStream.read()
返回整个缓冲区的长度),但接受了部分内容(由于长度头)。在开始读取16字节的消息并将其从缓冲区中清除后,它再次读取其空间中的零


解决方案是什么?让服务器在密码共享和连接的消息之间喘口气。原来这是一个“好”问题——我的手机太快了。这就是为什么它在一个使用旧软件的模拟器中工作。(加上我有一个闪电般快速的Nexus 4,这可能是原因之一。)

如果您在客户端Android端遇到任何异常,在描述的握手中是否有任何地方出现故障,并且您在操作过程中是否持有Android wakelock?安卓端不会抛出异常。我没有设置唤醒锁,我编辑了关于握手失败的地方的问题,因为我认为这可能会澄清这一点。你不使用SSL有什么好的理由吗?真的没有什么好的理由。我基本上想要一种简单快速的方法来实现nio服务器/客户机。这就是为什么我没有直接使用java加密的原因——我上一次的尝试就是这样做的,它抛出了一堆非法的块异常。Jayspt处理这个问题,在功能上包含了比我想象的更多的加密技术。至于SSL,在我的学习中,我还没有完全做到这一点。显然,nio对我来说是新来的,所以下一个就是它