nio - 使用java7的AIO异步读取数据时发现有重复读取的现象
伊谢尔伦
伊谢尔伦 2017-04-17 15:04:56
0
1
414

使用如下代码读取socket channel中的数据到缓冲区中,程序对读取到的数据进行处理。
如果读取到的一批数据中,存在半个message的情况下,程序会将本次剩余的半个message数据保存起来,
等下次读取到数据后,进行合并,然后判断程序是否能正常执行,若不能则表示第二次读取到的数据与
之前保存起来的半个message数据不是同一批,则程序继续重新处理第二次读取到的数据,然后第三次读取
后再合并,再试着处理...直到合并后能成功处理。

现在发现,合并后能成功处理,但是后面仍然会读取到已经处理过的半个消息的后半段数据,不知道各位大侠
有没有遇见过这种情况?

socket.read(byteBuffer, null, new CompletionHandler() { @Override public void completed(Integer result, Void attachment) { if(result==-1){ close(); return; } byteBuffer.clear(); if (result > 0) { byteBuffer.limit(result); LOG.debug("server socket [{}] get " + result + " data from client:{}", clientIdentifier, getString(byteBuffer, result)); if (!connectted) { handleConnect(); } else { if (remainingData != null) { combineBuffer(); } while (byteBuffer.position() < result) { doProcess(); } } } byteBuffer.clear(); socket.read(byteBuffer, null, this); } @Override public void failed(Throwable exc, Void attachment) { LOG.error("Server socket [{}] read failed,{}.", clientIdentifier, exc.toString()); close(); } });
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all (1)
左手右手慢动作

Another problem is that the number of bytes read each time is different. My message size is set to 2kb. Normally, every time the client sends a message, the server will read to 2kb. The data, but occasionally it only reads to 1kb or a few hundred bytes. Does anyone know what is going on?

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!