Answer the question
In order to leave comments, you need to log in
AsynchronousSocketChannel cannot read the response, what's the problem?
I'm trying to organize a poll for a large number of devices, I use AsynchronousSocketChannel + AsynchronousChannelGroup as a connection, due to the heavy load, sometimes the data does not come complete, code:
Creating a connection:
AsynchronousChannelGroup group = AsynchronousChannelGroup
.withFixedThreadPool(numThreads, Executors.defaultThreadFactory());
AsynchronousSocketChannel[] channels =
new AsynchronousSocketChannel[numToOpen];
for (int i = 0; i < numToOpen; i++) {
ByteBuffer request = ByteBuffer.wrap(Config.MESSAGE);
channels[i] = AsynchronousSocketChannel.open(group);
channels[i].setOption(StandardSocketOptions.SO_RCVBUF, 65536);
channels[i].setOption(StandardSocketOptions.SO_SNDBUF, 65536);
channels[i].setOption(StandardSocketOptions.SO_REUSEADDR, true);
channels[i].setOption(StandardSocketOptions.SO_KEEPALIVE, true);
channels[i].connect(new InetSocketAddress(device.getIp(), Config.PORT), new BufferContainer(request, channels[i], device,xmlQueue), new ConnectHandler());
}
public void completed(Void result, BufferContainer container) {
container.channel().write(container.buffer(), container, new WriteHandler());
}
public void completed(Integer arg, BufferContainer container) {
if (container.buffer().hasRemaining()) {
container.channel().write(
container.buffer(), container, this);
return;
}
ByteBuffer readbuff = ByteBuffer.allocateDirect(65536);
container.setBuffer(readbuff);
container.channel().read(readbuff, container, new ReadHandler());
}
public void completed(Integer result, BufferContainer container) {
//Тут не приходит result=-1, если вызвать :
//if(result != -1){
// container.channel().read(container.buffer(), container, this);
//}
// То второго вызова completed не происходит
container.buffer().flip();
strBuilder.append(charset.decode(container.buffer()));
strBuilder.delete(0, 5);
strBuilder.delete(strBuilder.length() - 2, strBuilder.length());
container.getXmlQueue().add(new XMLData(container.device() , strBuilder.toString()));
}
Answer the question
In order to leave comments, you need to log in
I can only assume that some data does not "fit" in one internal buffer and therefore
can be transferred to 2,3, ... calls.
Simply put, you need to buffer the received data somewhere before processing it (I don’t know what you have there is an indicator of the end of the message according to logic).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question