D
D
Dmitry2014-08-22 11:34:16
Java
Dmitry, 2014-08-22 11:34:16

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());
}

ConnectHandler :
public void completed(Void result, BufferContainer container) {
  container.channel().write(container.buffer(), container, new WriteHandler());		
}


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());
}


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()));
}


I tried to add Thread.sleep () in ReadHandler, in this case the data is almost always read to the end, but performance is cut at times.

Please tell me what could be the reason for obtaining incomplete data, and in which direction to dig.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
ponkin, 2014-09-23
@ponkin

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 question

Ask a Question

731 491 924 answers to any question